Game Development Reference
In-Depth Information
if instruction to find out which color to return. Just as for a method that has a return value, you use
the return keyword to indicate what value the property should return:
Object.defineProperty(Cannon.prototype, "color",
{
get: function () {
if (this.currentColor === sprites.cannon_red)
return Color.red;
else if (this.currentColor === sprites.cannon_green)
return Color.green;
else
return Color.blue;
}
});
You can now use this property to access the color of the cannon. For example, you can store it in a
variable, like this:
var cannonColor = cannon.Color;
You also want to be able to assign a value to the cannon color. For that, you have to define the
set part of the property. In that part, you need to modify the value of the currentColor variable.
This value is provided when the property is used in another method. For example, it could be an
instruction like this:
cannon.color = Color.Red;
Again, you use an if instruction to determine what the new value of the currentColor variable
should be. The right side of the assignment is passed to the set part as a parameter . The complete
property is then given as follows:
Object.defineProperty(Cannon.prototype, "color",
{
get: function () {;
if (this.currentColor === sprites.cannon_red)
return Color.red;
else if (this.currentColor === sprites.cannon_green)
return Color.green;
else
return Color.blue;
},
set: function (value) {
if (value === Color.red)
this.currentColor = sprites.cannon_red;
else if (value === Color.green)
this.currentColor = sprites.cannon_green;
else if (value === Color.blue)
this.currentColor = sprites.cannon_blue;
}
});
 
Search WWH ::




Custom Search