Game Development Reference
In-Depth Information
Figure 14-14. The potion button will replenish hit points
Creating Custom Events
Custom events can be very handy when you need to easily pass values around in your application. When an instance
of your custom event class is created, you can pass in extra parameters into its constructor. This custom class should
be written to accept and store those values accordingly. The event is created much like custom display objects are in
EaselJS. This class will be written in a file named events.js . This file is set up to write this custom event class, as well
as hold the event strings that you've seen throughout the game so far. Listing 14-32 shows the entire events.js file.
Listing 14-32. The events.js File Declares The Game's Event Types and the Custom BattleButtonEvent Event Class
window.events = window.events || {};
events.ATTACK_BUTTON_SELECTED = 'attack button selected';
events.ENEMY_ATTACK_ANIMATION_COMPLETE = 'enemy attack animation complete';
events.ENEMY_ATTACKED_COMPLETE = 'enemy attacked complete';
events.ENEMY_DESTROYED = 'enemy destroyed';
(function(){
function BattleButtonEvent(type,bubbles,cancelable,attackType){
this.attackType = attackType;
this.initialize(type,bubbles,cancelable);
}
var p = BattleButtonEvent.prototype = new createjs.Event();
p.attackType = null;
p.Event_initialize = p.initialize;
p.initialize = function(type,bubbles,cancelable){
this.Event_initialize(type,bubbles,cancelable);
}
window.events.BattleButtonEvent = BattleButtonEvent;
}());
As usual, the custom class is wrapped inside of a closure. Its structure is identical to those classes that extend
from other CreateJS classes. Extending Event requires that you accept at least the first type parameter that is used in
the Event class that you are extending. You'll need to pass this into the initialize function of Event , much like you
need to pass in a sprite sheet object when building a custom sprite.
 
Search WWH ::




Custom Search