我已經使這個對象處理我的遊戲中的不同選項。對於對象內的每個屬性
var options =
{
Option: function(name, value, shortcut)
{
this.name = name;
this.value = value;
this.shortcut = shortcut;
this.texte = createElement("span",this.name + " : " + this.shortcut + "<br />");
this.texte.parent("options");
this.texte.id(this.name);
},
toggle: function(shortcut)
{
for (var o in this)
{
console.log(o);
console.log(o.shortcut);
if (o.shortcut == shortcut)
{
o.value = !o.value;
if (o.value)
{
o.texte.style("fontWeight","bold");
o.texte.style("color","green");
}
else
{
o.texte.style("fontWeight","normal");
o.texte.style("color","red");
}
addText("Toggled"+ o.name);
}
}
},
initiate: function()
{
this.randomSpeed = new this.Option("Random Speed", true,"R");
//this.randomSpeed.show();
this.oneBall = new this.Option("Spawn one ball", false,"O");
//this.oneBall.show();
this.gravity = new this.Option("gravity", false,"G");
//this.gravity.show();
this.collision = new this.Option("Collision", false,"C");
//this.collision.show();
this.paintBackground = new this.Option("Paint mode",false,"P");
//this.paintBackground.show();
this.wall = new this.Option("Wall Collision", false,"W");
//this.wall.show();
this.unstuck = new this.Option("Unstuck", false,"U");
//this.unstuck.show();
this.blow = new this.Option("Mouse blow", false,"B");
//this.blow.show();
this.attraction = new this.Option("Mouse Attraction", false,"A");
//this.attraction.show();
this.superAttraction = new this.Option("Super attraction", false,"A");
//this.superAttraction.show();
}
};
目的是來處理,我可以添加後和使用已經存在相同的代碼新的選擇方式,所有的遊戲型動物的選項。
我使用創建者函數Option(在主對象選項內)在實現它們到遊戲中時創建新選項。
我的問題是與for (var o in this)
循環。
console.log(o);
給我我所期望的輸出
「選項」 「切換」, 「啓動」, 「randomSpeed」 「oneBall」 「萬有引力」 「碰撞」 「paintBackground」 「牆」, 「脫膠」,「打擊「」景點「 」superAttraction「
但是然後console.log(o.shortcut);
不返回任何東西! 我檢查了console of my browser,該物體存在,並且正確地實例化了
謝謝!
----------------------------
此外,我正在尋找這種設計的例子,但我無法找到與此相近的任何東西。你有我可以參考的例子嗎?我真的好奇,看看它通常是如何做到的。
你能提供工作的jsfiddle或者您的代碼部署 – GraveyardQueen
這是當前版本http://gosuness.free.fr/ballsOptions/ 這最新的一個url工作版本http://gosuness.free.fr/balls/ – Albizia