diff --git a/README.md b/README.md index 25f4dda5..d4cd1892 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ New features: * Phaser.Timer.stop has a new parameter: clearEvents (default true), if true all the events in Timer will be cleared, otherwise they will remain (fixes #383) * All GameObjects now have a 'destroyChildren' boolean as a parameter to their destroy method. It's default is true and the value propogates down its children. * Pixi GrayFilter ported over (thanks nickryall #404) +* Animation.speed added. You can now change the animation speed on the fly, without re-starting the animation (feature request #458) Updates: diff --git a/examples/wip/anim-speed.js b/examples/wip/anim-speed.js new file mode 100644 index 00000000..89ecf269 --- /dev/null +++ b/examples/wip/anim-speed.js @@ -0,0 +1,48 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18); + +} + +var mummy; +var anim; + +function create() { + + game.stage.backgroundColor = 0x2d2d2d; + + mummy = game.add.sprite(300, 200, 'mummy', 5); + + anim = mummy.animations.add('walk'); + + anim.play(10, true); + + game.input.onDown.add(changeSpeed, this); + +} + +function changeSpeed(pointer) { + + if (pointer.x < 400) + { + anim.speed -= 1; + } + else + { + anim.speed += 1; + } + +} + +function update() { + +} + +function render() { + + game.debug.renderText(anim.speed, 32, 32); + +} \ No newline at end of file diff --git a/src/animation/Animation.js b/src/animation/Animation.js index 52dc51fb..b1e22387 100644 --- a/src/animation/Animation.js +++ b/src/animation/Animation.js @@ -466,6 +466,29 @@ Object.defineProperty(Phaser.Animation.prototype, 'frame', { }); +/** +* @name Phaser.Animation#speed +* @property {number} speed - Gets or sets the current speed of the animation, the time between each frame of the animation, given in ms. Takes effect from the NEXT frame. Minimum value is 1. +*/ +Object.defineProperty(Phaser.Animation.prototype, 'speed', { + + get: function () { + + return Math.round(1000 / this.delay); + + }, + + set: function (value) { + + if (value >= 1) + { + this.delay = 1000 / value; + } + + } + +}); + /** * Really handy function for when you are creating arrays of animation data but it's using frame names and not numbers. * For example imagine you've got 30 frames named: 'explosion_0001-large' to 'explosion_0030-large'