Skip to content

Commit 11ed074

Browse files
committed
Refactoring how events are handled internally and in the docs
1 parent 79dc611 commit 11ed074

28 files changed

Lines changed: 390 additions & 163 deletions

src/animations/Animation.js

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,11 @@
77
var Clamp = require('../math/Clamp');
88
var Class = require('../utils/Class');
99
var EventEmitter = require('eventemitter3');
10+
var Events = require('./events');
1011
var FindClosestInSorted = require('../utils/array/FindClosestInSorted');
1112
var Frame = require('./AnimationFrame');
1213
var GetValue = require('../utils/object/GetValue');
1314

14-
/**
15-
* @typedef {object} JSONAnimation
16-
*
17-
* @property {string} key - The key that the animation will be associated with. i.e. sprite.animations.play(key)
18-
* @property {string} type - A frame based animation (as opposed to a bone based animation)
19-
* @property {JSONAnimationFrame[]} frames - [description]
20-
* @property {integer} frameRate - The frame rate of playback in frames per second (default 24 if duration is null)
21-
* @property {integer} duration - How long the animation should play for in milliseconds. If not given its derived from frameRate.
22-
* @property {boolean} skipMissedFrames - Skip frames if the time lags, or always advanced anyway?
23-
* @property {integer} delay - Delay before starting playback. Value given in milliseconds.
24-
* @property {integer} repeat - Number of times to repeat the animation (-1 for infinity)
25-
* @property {integer} repeatDelay - Delay before the animation repeats. Value given in milliseconds.
26-
* @property {boolean} yoyo - Should the animation yoyo? (reverse back down to the start) before repeating?
27-
* @property {boolean} showOnStart - Should sprite.visible = true when the animation starts to play?
28-
* @property {boolean} hideOnComplete - Should sprite.visible = false when the animation finishes?
29-
*/
30-
31-
/**
32-
* @typedef {object} AnimationFrameConfig
33-
*
34-
* @property {string} key - The key that the animation will be associated with. i.e. sprite.animations.play(key)
35-
* @property {(string|number)} frame - [description]
36-
* @property {number} [duration=0] - [description]
37-
* @property {boolean} [visible] - [description]
38-
*/
39-
40-
/**
41-
* @typedef {object} AnimationConfig
42-
*
43-
* @property {string} [key] - The key that the animation will be associated with. i.e. sprite.animations.play(key)
44-
* @property {AnimationFrameConfig[]} [frames] - An object containing data used to generate the frames for the animation
45-
* @property {string} [defaultTextureKey=null] - The key of the texture all frames of the animation will use. Can be overridden on a per frame basis.
46-
* @property {integer} [frameRate] - The frame rate of playback in frames per second (default 24 if duration is null)
47-
* @property {integer} [duration] - How long the animation should play for in milliseconds. If not given its derived from frameRate.
48-
* @property {boolean} [skipMissedFrames=true] - Skip frames if the time lags, or always advanced anyway?
49-
* @property {integer} [delay=0] - Delay before starting playback. Value given in milliseconds.
50-
* @property {integer} [repeat=0] - Number of times to repeat the animation (-1 for infinity)
51-
* @property {integer} [repeatDelay=0] - Delay before the animation repeats. Value given in milliseconds.
52-
* @property {boolean} [yoyo=false] - Should the animation yoyo? (reverse back down to the start) before repeating?
53-
* @property {boolean} [showOnStart=false] - Should sprite.visible = true when the animation starts to play?
54-
* @property {boolean} [hideOnComplete=false] - Should sprite.visible = false when the animation finishes?
55-
*/
56-
5715
/**
5816
* @classdesc
5917
* A Frame based Animation.
@@ -72,7 +30,7 @@ var GetValue = require('../utils/object/GetValue');
7230
*
7331
* @param {Phaser.Animations.AnimationManager} manager - [description]
7432
* @param {string} key - [description]
75-
* @param {AnimationConfig} config - [description]
33+
* @param {Phaser.Animations.Animation.Config} config - [description]
7634
*/
7735
var Animation = new Class({
7836

@@ -266,7 +224,7 @@ var Animation = new Class({
266224
* @method Phaser.Animations.Animation#addFrame
267225
* @since 3.0.0
268226
*
269-
* @param {(string|AnimationFrameConfig[])} config - [description]
227+
* @param {(string|Phaser.Animations.AnimationFrame.Config[])} config - [description]
270228
*
271229
* @return {Phaser.Animations.Animation} This Animation object.
272230
*/
@@ -282,7 +240,7 @@ var Animation = new Class({
282240
* @since 3.0.0
283241
*
284242
* @param {integer} index - [description]
285-
* @param {(string|AnimationFrameConfig[])} config - [description]
243+
* @param {(string|Phaser.Animations.AnimationFrame.Config[])} config - [description]
286244
*
287245
* @return {Phaser.Animations.Animation} This Animation object.
288246
*/
@@ -395,7 +353,7 @@ var Animation = new Class({
395353
* @since 3.0.0
396354
*
397355
* @param {Phaser.Textures.TextureManager} textureManager - [description]
398-
* @param {(string|AnimationFrameConfig[])} frames - [description]
356+
* @param {(string|Phaser.Animations.AnimationFrame.Config[])} frames - [description]
399357
* @param {string} [defaultTextureKey] - [description]
400358
*
401359
* @return {Phaser.Animations.AnimationFrame[]} [description]
@@ -774,6 +732,9 @@ var Animation = new Class({
774732
* [description]
775733
*
776734
* @method Phaser.Animations.Animation#repeatAnimation
735+
* @fires Phaser.Animations.Events#ANIMATION_REPEAT
736+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_REPEAT
737+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_KEY_REPEAT
777738
* @since 3.0.0
778739
*
779740
* @param {Phaser.GameObjects.Components.Animation} component - [description]
@@ -806,11 +767,11 @@ var Animation = new Class({
806767
var frame = component.currentFrame;
807768
var parent = component.parent;
808769

809-
this.emit('repeat', this, frame);
770+
this.emit(Events.ANIMATION_REPEAT, this, frame);
810771

811-
parent.emit('animationrepeat-' + this.key, this, frame, component.repeatCounter, parent);
772+
parent.emit(Events.SPRITE_ANIMATION_KEY_REPEAT + this.key, this, frame, component.repeatCounter, parent);
812773

813-
parent.emit('animationrepeat', this, frame, component.repeatCounter, parent);
774+
parent.emit(Events.SPRITE_ANIMATION_REPEAT, this, frame, component.repeatCounter, parent);
814775
}
815776
}
816777
},

src/animations/AnimationFrame.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66

77
var Class = require('../utils/Class');
88

9-
/**
10-
* @typedef {object} JSONAnimationFrame
11-
*
12-
* @property {string} key - The key of the Texture this AnimationFrame uses.
13-
* @property {(string|integer)} frame - The key of the Frame within the Texture that this AnimationFrame uses.
14-
* @property {number} duration - Additional time (in ms) that this frame should appear for during playback.
15-
*/
16-
179
/**
1810
* @classdesc
1911
* A single frame in an Animation sequence.
@@ -149,7 +141,7 @@ var AnimationFrame = new Class({
149141
* @method Phaser.Animations.AnimationFrame#toJSON
150142
* @since 3.0.0
151143
*
152-
* @return {JSONAnimationFrame} The AnimationFrame data.
144+
* @return {Phaser.Animations.AnimationFrame.JSONConfig} The AnimationFrame data.
153145
*/
154146
toJSON: function ()
155147
{

src/animations/AnimationManager.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Animation = require('./Animation');
88
var Class = require('../utils/Class');
99
var CustomMap = require('../structs/Map');
1010
var EventEmitter = require('eventemitter3');
11+
var Events = require('./events');
1112
var GetValue = require('../utils/object/GetValue');
1213
var Pad = require('../utils/string/Pad');
1314

@@ -129,7 +130,7 @@ var AnimationManager = new Class({
129130
* Adds an existing Animation to the Animation Manager.
130131
*
131132
* @method Phaser.Animations.AnimationManager#add
132-
* @fires AddAnimationEvent
133+
* @fires Phaser.Animations.Events#ADD_ANIMATION
133134
* @since 3.0.0
134135
*
135136
* @param {string} key - The key under which the Animation should be added. The Animation will be updated with it. Must be unique.
@@ -141,15 +142,16 @@ var AnimationManager = new Class({
141142
{
142143
if (this.anims.has(key))
143144
{
144-
console.warn('Animation with key', key, 'already exists');
145+
console.warn('Animation key exists: ' + key);
146+
145147
return;
146148
}
147149

148150
animation.key = key;
149151

150152
this.anims.set(key, animation);
151153

152-
this.emit('add', key, animation);
154+
this.emit(Events.ADD_ANIMATION, key, animation);
153155

154156
return this;
155157
},
@@ -185,7 +187,7 @@ var AnimationManager = new Class({
185187
* If you wish to re-use an existing key, call `AnimationManager.remove` first, then this method.
186188
*
187189
* @method Phaser.Animations.AnimationManager#create
188-
* @fires AddAnimationEvent
190+
* @fires Phaser.Animations.Events#ADD_ANIMATION
189191
* @since 3.0.0
190192
*
191193
* @param {AnimationConfig} config - The configuration settings for the Animation.
@@ -208,7 +210,7 @@ var AnimationManager = new Class({
208210

209211
this.anims.set(key, anim);
210212

211-
this.emit('add', key, anim);
213+
this.emit(Events.ADD_ANIMATION, key, anim);
212214
}
213215
}
214216

@@ -470,7 +472,7 @@ var AnimationManager = new Class({
470472
* Pause all animations.
471473
*
472474
* @method Phaser.Animations.AnimationManager#pauseAll
473-
* @fires PauseAllAnimationEvent
475+
* @fires Phaser.Animations.Events#PAUSE_ALL
474476
* @since 3.0.0
475477
*
476478
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
@@ -481,7 +483,7 @@ var AnimationManager = new Class({
481483
{
482484
this.paused = true;
483485

484-
this.emit('pauseall');
486+
this.emit(Events.PAUSE_ALL);
485487
}
486488

487489
return this;
@@ -524,7 +526,7 @@ var AnimationManager = new Class({
524526
* Remove an animation.
525527
*
526528
* @method Phaser.Animations.AnimationManager#remove
527-
* @fires RemoveAnimationEvent
529+
* @fires Phaser.Animations.Events#REMOVE_ANIMATION
528530
* @since 3.0.0
529531
*
530532
* @param {string} key - The key of the animation to remove.
@@ -537,7 +539,7 @@ var AnimationManager = new Class({
537539

538540
if (anim)
539541
{
540-
this.emit('remove', key, anim);
542+
this.emit(Events.REMOVE_ANIMATION, key, anim);
541543

542544
this.anims.delete(key);
543545
}
@@ -549,7 +551,7 @@ var AnimationManager = new Class({
549551
* Resume all paused animations.
550552
*
551553
* @method Phaser.Animations.AnimationManager#resumeAll
552-
* @fires ResumeAllAnimationEvent
554+
* @fires Phaser.Animations.Events#RESUME_ALL
553555
* @since 3.0.0
554556
*
555557
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
@@ -560,7 +562,7 @@ var AnimationManager = new Class({
560562
{
561563
this.paused = false;
562564

563-
this.emit('resumeall');
565+
this.emit(Events.RESUME_ALL);
564566
}
565567

566568
return this;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* The Add Animation Event.
3+
*
4+
* This event is dispatched when a new animation is added to the global Animation Manager.
5+
*
6+
* This can happen either as a result of an animation instance being added to the Animation Manager,
7+
* or the Animation Manager creating a new animation directly.
8+
*
9+
* @event Phaser.Animations.Events#ADD_ANIMATION
10+
*
11+
* @param {string} key - The key of the Animation that was added to the global Animation Manager.
12+
* @param {Phaser.Animations.Animation} animation - An instance of the newly created Animation.
13+
*/
14+
module.exports = 'add';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* The Animation Complete Event.
3+
*
4+
* This event is dispatched by an Animation instance when it completes, i.e. finishes playing or is manually stopped.
5+
*
6+
* Be careful with the volume of events this could generate. If a group of Sprites all complete the same
7+
* animation at the same time, this event will invoke its handler for each one of them.
8+
*
9+
* @event Phaser.Animations.Events#ANIMATION_COMPLETE
10+
*
11+
* @param {Phaser.Animations.Animation} animation - A reference to the Animation that completed.
12+
* @param {Phaser.Animations.AnimationFrame} frame - The current Animation Frame that the Animation completed on.
13+
* @param {Phaser.GameObjects.Sprite} gameObject - A reference to the Game Object on which the animation completed.
14+
*/
15+
module.exports = 'complete';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* The Animation Repeat Event.
3+
*
4+
* This event is dispatched when a currently playing animation repeats.
5+
*
6+
* The event is dispatched directly from the Animation object itself. Which means that listeners
7+
* bound to this event will be invoked every time the Animation repeats, for every Game Object that may have it.
8+
*
9+
* @event Phaser.Animations.Events#ANIMATION_REPEAT
10+
*
11+
* @param {Phaser.Animations.Animation} animation - A reference to the Animation that repeated.
12+
* @param {Phaser.Animations.AnimationFrame} frame - The current Animation Frame that the Animation was on when it repeated.
13+
*/
14+
module.exports = 'repeat';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* The Animation Restart Event.
3+
*
4+
* This event is dispatched by an Animation instance when it restarts.
5+
*
6+
* Be careful with the volume of events this could generate. If a group of Sprites all restart the same
7+
* animation at the same time, this event will invoke its handler for each one of them.
8+
*
9+
* @event Phaser.Animations.Events#ANIMATION_RESTART
10+
*
11+
* @param {Phaser.Animations.Animation} animation - A reference to the Animation that restarted playing.
12+
* @param {Phaser.Animations.AnimationFrame} frame - The current Animation Frame that the Animation restarted with.
13+
* @param {Phaser.GameObjects.Sprite} gameObject - A reference to the Game Object on which the animation restarted playing.
14+
*/
15+
module.exports = 'restart';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* The Animation Start Event.
3+
*
4+
* This event is dispatched by an Animation instance when it starts playing.
5+
*
6+
* Be careful with the volume of events this could generate. If a group of Sprites all play the same
7+
* animation at the same time, this event will invoke its handler for each one of them.
8+
*
9+
* @event Phaser.Animations.Events#ANIMATION_START
10+
*
11+
* @param {Phaser.Animations.Animation} animation - A reference to the Animation that started playing.
12+
* @param {Phaser.Animations.AnimationFrame} frame - The current Animation Frame that the Animation started with.
13+
* @param {Phaser.GameObjects.Sprite} gameObject - A reference to the Game Object on which the animation started playing.
14+
*/
15+
module.exports = 'start';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* The Pause All Animations Event.
3+
*
4+
* This event is dispatched when the global Animation Manager is told to pause.
5+
*
6+
* When this happens all current animations will stop updating, although it doesn't necessarily mean
7+
* that the game has paused as well.
8+
*
9+
* @event Phaser.Animations.Events#PAUSE_ALL
10+
*/
11+
module.exports = 'pauseall';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* The Remove Animation Event.
3+
*
4+
* This event is dispatched when an animation is removed from the global Animation Manager.
5+
*
6+
* @event Phaser.Animations.Events#REMOVE_ANIMATION
7+
*
8+
* @param {string} key - The key of the Animation that was removed from the global Animation Manager.
9+
* @param {Phaser.Animations.Animation} animation - An instance of the removed Animation.
10+
*/
11+
module.exports = 'remove';

0 commit comments

Comments
 (0)