2012-09-17 22 views
0

我不能得到這個工作:推新功能到一個數組 - 的Javascript

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

警報(_this.colour)只是提醒不確定。有任何想法嗎?

+0

'load_players'不叫? –

+0

我更新了代碼以表示我將它稱爲的位置......並且再次。嘿嘿 – Jimmyt1988

回答

2
  1. 你試圖在致電之前循環
  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(); 
} 
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' 
     }) 
    ); 
} 

P.S.不要使用for...in作爲陣列。只需使用正常for即可。

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

P.P.S.您需要將this.colour = properties.colour;添加到您的player構造函數中。

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/

+0

哦,上帝..顏色沒有被設置..多麼尷尬..非常感謝你抽出時間來出我的noobish學校男孩作爲noob的錯誤。 toolateforcode.com – Jimmyt1988

+0

我將其更改爲:global_variables.players = [ 新玩家(... 再次感謝 – Jimmyt1988