Skip to content

Commit b4b5338

Browse files
committed
Added playReverse, delayedPlay and stop methods and better docs.
1 parent 33cc871 commit b4b5338

1 file changed

Lines changed: 130 additions & 6 deletions

File tree

src/gameobjects/sprite/Sprite.js

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,39 @@ var Sprite = new Class({
134134
},
135135

136136
/**
137-
* Start playing the given animation.
137+
* Start playing the given animation on this Sprite.
138+
*
139+
* Animations in Phaser belong to the global Animation Manager. This means multiple Sprites can all play the same
140+
* animation. The following code shows how to create a global repeating animation. The animation will be created
141+
* from all of the frames within the sprite sheet that was loaded with the key 'muybridge':
142+
*
143+
* ```javascript
144+
* var config = {
145+
* key: 'run',
146+
* frames: 'muybridge',
147+
* frameRate: 15,
148+
* repeat: -1
149+
* };
150+
*
151+
* this.anims.create(config);
152+
* ```
153+
*
154+
* With the animation created, you can now play it on this Sprite:
155+
*
156+
* ```javascript
157+
* this.add.sprite(x, y).play('run');
158+
* ```
159+
*
160+
* Alternatively, if you wish to run it at a different frame rate, for example, you can pass a config
161+
* object instead:
162+
*
163+
* ```javascript
164+
* this.add.sprite(x, y).play({ key: 'run', frameRate: 24 });
165+
* ```
166+
*
167+
* See the documentation for the `PlayAnimationConfig` config object for more details about this.
168+
*
169+
* Also, see the documentation in the Animation Manager for further details on creating animations.
138170
*
139171
* @method Phaser.GameObjects.Sprite#play
140172
* @fires Phaser.Animations.Events#ANIMATION_START
@@ -153,6 +185,102 @@ var Sprite = new Class({
153185
return this.anims.play(key, ignoreIfPlaying, startFrame);
154186
},
155187

188+
/**
189+
* Start playing the given animation on this Sprite in reverse.
190+
*
191+
* Animations in Phaser belong to the global Animation Manager. This means multiple Sprites can all play the same
192+
* animation. The following code shows how to create a global repeating animation. The animation will be created
193+
* from all of the frames within the sprite sheet that was loaded with the key 'muybridge':
194+
*
195+
* ```javascript
196+
* var config = {
197+
* key: 'run',
198+
* frames: 'muybridge',
199+
* frameRate: 15,
200+
* repeat: -1
201+
* };
202+
*
203+
* this.anims.create(config);
204+
* ```
205+
*
206+
* With the animation created, you can now play it on this Sprite:
207+
*
208+
* ```javascript
209+
* this.add.sprite(x, y).playReverse('run');
210+
* ```
211+
*
212+
* Alternatively, if you wish to run it at a different frame rate, for example, you can pass a config
213+
* object instead:
214+
*
215+
* ```javascript
216+
* this.add.sprite(x, y).playReverse({ key: 'run', frameRate: 24 });
217+
* ```
218+
*
219+
* See the documentation for the `PlayAnimationConfig` config object for more details about this.
220+
*
221+
* Also, see the documentation in the Animation Manager for further details on creating animations.
222+
*
223+
* @method Phaser.GameObjects.Sprite#playReverse
224+
* @fires Phaser.Animations.Events#ANIMATION_START
225+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_START
226+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_KEY_START
227+
* @since 3.50.0
228+
*
229+
* @param {(string|Phaser.Animations.Animation|Phaser.Types.Animations.PlayAnimationConfig)} key - The string-based key of the animation to play, or an Animation instance, or a `PlayAnimationConfig` object.
230+
* @param {boolean} [ignoreIfPlaying=false] - If an animation is already playing then ignore this call.
231+
* @param {integer} [startFrame=0] - Optionally start the animation playing from this frame index.
232+
*
233+
* @return {this} This Game Object.
234+
*/
235+
playReverse: function (key, ignoreIfPlaying, startFrame)
236+
{
237+
return this.anims.playReverse(key, ignoreIfPlaying, startFrame);
238+
},
239+
240+
/**
241+
* Waits for the specified delay, in milliseconds, then starts playback of the requested animation.
242+
*
243+
* If the animation _also_ has a delay value set in its config, this value will override that and be used instead.
244+
*
245+
* The delay only takes effect if the animation has not already started playing.
246+
*
247+
* @method Phaser.GameObjects.Sprite#delayedPlay
248+
* @fires Phaser.Animations.Events#ANIMATION_START
249+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_START
250+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_KEY_START
251+
* @since 3.50.0
252+
*
253+
* @param {integer} delay - The delay, in milliseconds, to wait before starting the animation playing.
254+
* @param {(string|Phaser.Animations.Animation|Phaser.Types.Animations.PlayAnimationConfig)} key - The string-based key of the animation to play, or an Animation instance, or a `PlayAnimationConfig` object.
255+
* @param {integer} [startFrame=0] - The frame of the animation to start from.
256+
*
257+
* @return {this} This Game Object.
258+
*/
259+
delayedPlay: function (delay, key, startFrame)
260+
{
261+
return this.anims.delayedPlay(delay, key, startFrame);
262+
},
263+
264+
/**
265+
* Immediately stops the current animation from playing and dispatches the `ANIMATION_STOP` events.
266+
*
267+
* If no animation is playing, no event will be dispatched.
268+
*
269+
* If there is another animation queued (via the `chain` method) then it will start playing immediately.
270+
*
271+
* @method Phaser.GameObjects.Sprite#stop
272+
* @fires Phaser.Animations.Events#ANIMATION_STOP
273+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_STOP
274+
* @fires Phaser.Animations.Events#SPRITE_ANIMATION_KEY_STOP
275+
* @since 3.50.0
276+
*
277+
* @return {this} This Game Object.
278+
*/
279+
stop: function ()
280+
{
281+
return this.anims.stop();
282+
},
283+
156284
/**
157285
* Build a JSON representation of this Sprite.
158286
*
@@ -163,11 +291,7 @@ var Sprite = new Class({
163291
*/
164292
toJSON: function ()
165293
{
166-
var data = Components.ToJSON(this);
167-
168-
// Extra Sprite data is added here
169-
170-
return data;
294+
return Components.ToJSON(this);
171295
},
172296

173297
/**

0 commit comments

Comments
 (0)