Game Development Reference
In-Depth Information
set: function (value) {
if (value >=0 && value < this.sprite.nrSheetElements)
this._sheetIndex = value;
}
});
Look at the
SpriteGameObject.js
file in the
PenguinPairs1
example to see a few other examples
of properties you can add. The final method of
SpriteGameObject
is
draw
, which is simple because
most of the work has already been done in the
SpriteSheet
class:
SpriteGameObject.prototype.draw = function () {
if (this._visible)
this.sprite.draw(this.worldPosition, this.origin, this._sheetIndex);
};
Finalizing the Example
In the
PenguinPairs1
example, you draw a background and a penguin on the screen (see Figure
18-2
).
To test your new
SpriteGameObject
class, modify the currently selected sheet index by pressing the
left and right arrow buttons. This is done easily in the
PenguinPairsGameWorld
class:
if (Keyboard.pressed(Keys.left))
this.penguin.sheetIndex--;
else if (Keyboard.pressed(Keys.right))
this.penguin.sheetIndex++;
Search WWH ::
Custom Search