Skip to content

Commit

Permalink
Animation.killOnComplete added and fixed a few issues in the Tanks game.
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Oct 9, 2013
1 parent 29acf7f commit f10f932
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Version 1.0.7 (in progress in the dev branch)
* velocityFromAngle and velocityFromRotation added with examples created.
* Fixed the RandomDataGenerator.sow method so if you give in the same seed you'll now get the same results (thanks Hsaka)
* World.randomX/Y now works with negative World.bounds values.
* Added killOnComplete parameter to Animation.play. Really useful in situations where you want a Sprite to animate once then kill itself on complete, like an explosion effect.



* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
Expand Down
13 changes: 7 additions & 6 deletions examples/games/tanks.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function preload () {
game.load.atlas('enemy', 'assets/games/tanks/enemy-tanks.png', 'assets/games/tanks/tanks.json');
game.load.image('bullet', 'assets/games/tanks/bullet.png');
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
game.load.spritesheet('explosion', 'assets/games/tanks/explosion.png', 64, 64, 23);
game.load.spritesheet('kaboom', 'assets/games/tanks/explosion.png', 64, 64, 23);

}

Expand Down Expand Up @@ -166,8 +166,9 @@ function create () {

for (var i = 0; i < 10; i++)
{
var e = explosions.create(0, 0, 'explosion', 0, false);
e.animations.add('boom');
var explosionAnimation = explosions.create(0, 0, 'kaboom', [0], false);
explosionAnimation.anchor.setTo(0.5, 0.5);
explosionAnimation.animations.add('kaboom');
}

tank.bringToTop();
Expand Down Expand Up @@ -258,9 +259,9 @@ function bulletHitEnemy (tank, bullet) {

if (destroyed)
{
var e = explosions.getFirstDead();
e.reset(tank.x, tank.y);
e.play('boom');
var explosionAnimation = explosions.getFirstDead();
explosionAnimation.reset(tank.x, tank.y);
explosionAnimation.play('kaboom', 30, false, true);
}

}
Expand Down
21 changes: 19 additions & 2 deletions src/animation/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ Phaser.Animation = function (game, parent, name, frameData, frames, delay, loope
*/
this.looped = looped;

/**
* @property {boolean} looped - The loop state of the Animation.
*/
this.killOnComplete = false;

/**
* @property {boolean} isFinished - The finished state of the Animation. Set to true once playback completes, false during playback.
* @default
Expand Down Expand Up @@ -120,10 +125,11 @@ Phaser.Animation.prototype = {
* @method Phaser.Animation#play
* @memberof Phaser.Animation
* @param {number} [frameRate=null] - The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frameRate of the Animation is used.
* @param {boolean} [loop=null] - Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [loop=false] - Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [killOnComplete=false] - If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed.
* @return {Phaser.Animation} - A reference to this Animation instance.
*/
play: function (frameRate, loop) {
play: function (frameRate, loop, killOnComplete) {

if (typeof frameRate === 'number')
{
Expand All @@ -137,6 +143,12 @@ Phaser.Animation.prototype = {
this.looped = loop;
}

if (typeof killOnComplete !== 'undefined')
{
// Remove the parent sprite once the animation has finished?
this.killOnComplete = killOnComplete;
}

this.isPlaying = true;
this.isFinished = false;

Expand Down Expand Up @@ -298,6 +310,11 @@ Phaser.Animation.prototype = {
this._parent.events.onAnimationComplete.dispatch(this._parent, this);
}

if (this.killOnComplete)
{
this._parent.kill();
}

}

};
Expand Down
11 changes: 6 additions & 5 deletions src/animation/AnimationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Phaser.AnimationManager.prototype = {
// If they didn't set the useNumericIndex then let's at least try and guess it
if (typeof useNumericIndex === 'undefined')
{
if (frames[0] && typeof frames[0] === 'number')
if (frames && frames[0] && typeof frames[0] === 'number')
{
useNumericIndex = true;
}
Expand Down Expand Up @@ -173,24 +173,25 @@ Phaser.AnimationManager.prototype = {
* @method Phaser.AnimationManager#play
* @param {string} name - The name of the animation to be played, e.g. "fire", "walk", "jump".
* @param {number} [frameRate=null] - The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frameRate of the Animation is used.
* @param {boolean} [loop=null] - Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [loop=false] - Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [killOnComplete=false] - If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed.
* @return {Phaser.Animation} A reference to playing Animation instance.
*/
play: function (name, frameRate, loop) {
play: function (name, frameRate, loop, killOnComplete) {

if (this._anims[name])
{
if (this.currentAnim == this._anims[name])
{
if (this.currentAnim.isPlaying == false)
{
return this.currentAnim.play(frameRate, loop);
return this.currentAnim.play(frameRate, loop, killOnComplete);
}
}
else
{
this.currentAnim = this._anims[name];
return this.currentAnim.play(frameRate, loop);
return this.currentAnim.play(frameRate, loop, killOnComplete);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/gameobjects/Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,15 @@ Phaser.Sprite.prototype.bringToTop = function() {
* @method play
* @param {String} name The name of the animation to be played, e.g. "fire", "walk", "jump".
* @param {number} [frameRate=null] The framerate to play the animation at. The speed is given in frames per second. If not provided the previously set frameRate of the Animation is used.
* @param {Boolean} [loop=null] Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [loop=false] Should the animation be looped after playback. If not provided the previously set loop value of the Animation is used.
* @param {boolean} [killOnComplete=false] - If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed.
* @return {Phaser.Animation} A reference to playing Animation instance.
*/
Phaser.Sprite.prototype.play = function (name, frameRate, loop) {
Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete) {

if (this.animations)
{
this.animations.play(name, frameRate, loop);
this.animations.play(name, frameRate, loop, killOnComplete);
}

}
Expand Down

0 comments on commit f10f932

Please sign in to comment.