Ich habe dieses Objekt gemacht, um verschiedene Optionen in meinem Spiel zu behandeln.Für jede Eigenschaft innerhalb eines Objekts
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();
}
};
Ziel war es, all differents Möglichkeiten des Spiels in einer Art und Weise zu handhaben, dass ich neue Optionen später und mit dem gleichen Code hinzufügen kann, die bereits vorhanden ist.
Ich benutze die Creator-Funktion Option (innerhalb der Hauptobjektoptionen), um neue Optionen zu installieren, wenn ich sie in das Spiel implementiere.
mein Problem ist mit der for (var o in this)
Schleife.
console.log(o);
gibt mir die Ausgabe I
"Option" "Toggle" "initiieren" "randomSpeed" "oneBall" "Schwerkraft" "Kollision" "Paintbackground" "Wand" "unstuck" „Schlag erwarten " "Attraktion" "superAttraction"
aber dann console.log(o.shortcut);
kehrt irgendetwas nicht! Ich habe in der console of my browser eingecheckt, das Objekt existiert, und ist korrekt instanziiert
Vielen Dank!
----------------------------
Außerdem bin ich zum Beispiel dieser Art von Design suchen, aber ich konnte nichts in der Nähe finden. Hast du ein Beispiel, auf das ich mich beziehen kann? Ich bin wirklich neugierig zu sehen, wie es normalerweise gemacht wird.
Kann Sie eine funktionierende jsfiddle oder eine URL in dem der Code – GraveyardQueen
entfaltet Dies ist die aktuelle Version http://gosuness.free.fr/ballsOptions/ Dies ist die neueste Arbeitsversion http://gosuness.free.fr/balls/ – Albizia