2016-05-15 74 views
-3
player.obj.speed = 20; 
computer1.obj.speed = 20; 
computer2.obj.speed = 20; 

通配符於JavaScript(也許這是沒有道理的?)

*.obj.speed =20; //once and for all! 

我覺得這裏我的問題是不夠清楚理解。 我會安裝自動化軟件包,如果需要......但我認爲有最簡單的方法,讓我facepalm ... 反正謝謝!

+2

與[player,computer1,computer2]組成一個數組,並循環每個項目並設置它們。 – YOU

回答

0
objectArray = [ //Array of the objects (not their speeds as integers don't get passed by reference, but objects do) 
    player.obj, 
    computer1.obj, 
    computer2.obj 
] 

function setAll(X) 
{ 
    //Go over all the objects 
    for(var i = 0; i < objectArray.length; i++) 
    { 
     objectArray[i].speed = X; 
    } 

} 

這很簡單:創建一個對象數組(將更新原始數據,如同整數或浮點數組一樣)。然後,當您想要更改它們時,只需遍歷數組並設置每個人的速度屬性即可。

+0

非常感謝...爲您簡單的解釋! –

相關問題