Game Development Reference
In-Depth Information
var p = Ship.prototype = new createjs.Sprite();
p.Sprite_initialize = p.initialize;
p.initialize = function (spritesheet) {
this.Sprite_initialize(spritesheet);
}
window.sprites.Ship = Ship;
}());
The sprite sheet object is passed into the instance of the sprite and is passed along to the initialize function
so it can be used when calling the super's initializing function. Note that your namespacing code is not necessary to
extend Sprite , but you will be using it here as you did with the UI elements to keep them out of the global space.
window.sprites = window.sprites || {};
Using Custom Sprites
As you would expect, using custom sprites in your application is as easy as using one out of the box. Your custom
sprite will need a sprite sheet, so be sure you have one ready before trying to add your new sprites to the stage.
The following is an example of a custom sprite being instantiated by passing it an existing sprite sheet:
var ship = new sprites.Ship(shipSpriteSheet);
addChild(ship);
Your custom sprites will often contain methods to act or animate in certain ways, depending on the situation in
your game. Listing 8-15 shows a custom property and some methods that might be added to a Ship class.
Listing 8-15. Example of Extending Sprite for a Ship Class
p.points = 10;
p.die = function(){
this.gotoAndPlay('explode');
this.on('animationend',this.destroy);
}
p.destroy = function(){
this.parent.removeChild(this);
this = null;
}
You can then use this ship in the following way:
score += ship.points;
ship.die();
This will increment a score variable by the value of the points property on the ship instance. The ship will
clean itself up by playing an explode sequence, then removing itself from the container it's in when the animation
has ended.
 
Search WWH ::




Custom Search