0

我不能让它工作:

var global_variables = {
    players: []
}; 

var player = function(properties){
    this.width = properties.width;                            
    this.height = properties.height;
    this.position = properties.position;
    this.position.x = properties.position.x;
    this.position.y = properties.position.y;
}

function load_players(){
    global_variables.players.push(
        [
            new player(
                {
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
                }
            )
        ]
    );
}


 function init(){ 
    var ctx = load_canvas();
    load_players();

    for(a in global_variables.players){
        var _this = global_variables.players[a];
        alert(_this.colour);
    }
}

alert(_this.colour) 只是提醒未定义。有任何想法吗?

4

3 回答 3

2

您从中获得undefined_this.colour原因有两个:

  1. 您没有colour在构造函数中设置属性
  2. 不是将每个新玩家推入数组,而是推入一个包含新玩家的单元素数组。也就是说,您正在创建一个数组数组。

将此添加到构造函数中:

    this.colour = properties.colour;

load_players()然后从函数中删除方括号:

function load_players(){
    global_variables.players.push(        
            new player({
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
            })
    );
}

演示:http: //jsfiddle.net/ZACnC/

于 2012-09-17T21:38:23.790 回答
2
  1. 您在调用之前尝试循环init
  2. 您正在推送包装在数组中的播放器实例。你只是想推动玩家。
  3. 不要for ... in ...在数组上使用。使用常规循环或forEach像我一样使用。
  4. 您从未真正将colour属性设置为您的实例。

http://jsfiddle.net/GLsR2/

这是小提琴的代码:

var global_variables = {
    players: []
}; 

var player = function(properties){
    this.width = properties.width;                            
    this.height = properties.height;
    this.position = properties.position;
    this.position.x = properties.position.x;
    this.position.y = properties.position.y;
    this.colour = properties.colour;
}

function load_players(){
    global_variables.players.push(
        new player(
                {
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
                }
            )
      );
}


init();

global_variables.players.forEach(function(player) {
   alert(player.colour); 
});

function init(){ 
    load_players();
}
于 2012-09-17T21:39:27.157 回答
0

当你推到 时global_variables.players,你推的是一个包含对象的数组new Player,而不是对象本身。

[]使用时不需要.push.

function load_players(){
    global_variables.players.push(
        new player({
            width: 10,
            height: 10,
            position: {
                x: 50,
                y: 50
            },
            colour: '#ff0000'
        })
    );
}

PS 不要for...in用于数组。只需使用普通的for.

for(var a = 0, len = global_variables.players.length; a < len; a++){
    var _this = global_variables.players[a];
    alert(_this.colour);
}

PPS 您需要添加this.colour = properties.colour;到您的player构造函数。

于 2012-09-17T21:37:56.833 回答