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) は undefined を警告するだけです。何か案は?

4

3 に答える 3

2

undefinedから取得する_this.colour理由は 2 つあります。

  1. colourコンストラクターでプロパティを設定しない
  2. 新しいプレーヤーをそれぞれ配列にプッシュするのではなく、新しいプレーヤーを含む 1 要素の配列をプッシュしています。つまり、配列の配列を作成しています。

これをコンストラクターに追加します。

    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'
        })
    );
}

for...inPS配列には使用しないでください。通常の を使用するだけ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 に答える