Menu button is an instance of GameButton class, which represents button element in game projects. Other game elements are instances of Sprite class, or of some custom class extended from Sprite class. Sprite class is a base class for all game objects.

Simple example is to create and draw a drop on the scene. The easiest way is to create Sprite instance with specific parameters and add it to the scene.
Example: create new instance of sprite class
var drop = new Sprite({image: “drop.png”, x: 200, y: 50});
scene.add(drop);
Mostly we want assign some properties to our object that are specific for it. Therefore we created our own class Drop in snake game, which is extended from Sprite. Now we can set images and specify our own properties in init method .
Example: create custom class extended from Sprite class
class Drop : Sprite
{
// Static variables
var _foodImage = Bitmap.fromFile(G_DROP_I);
// // Construction
function init()
{
super.init(); this._score = 10;
this._speedValue = 5; this.image = this._foodImage;
}
// // Properties
property score(v)
{
get return this._score;
} property speedValue(v)
{
get return this._speedValue;
}
//
}
Another advantage is that we can load image to the static variable _foodImage and so when we create more drop objects, we’ll hold just one instance of this image in memory.
Other two game elements: Snake and Score indicator - are described in separate class.
|