Jump to content

Play Animation and go to start


Recommended Posts

Hi there,


I have a fundamental question about Phaser Sprites and Animations.


What I want is to play an animation and then go back to the first frame of an spritesheet. The first frame of the spritesheet however is not part of the animation. Some code here:


figure1 = game.add.sprite(300, 1400, 'figure1');figure1.animations.add('stand', [0], 1, false);figure1.animations.add('dosomething', [1,2,3,4,5,6], 6, false);figure1.inputEnabled = true;figure1.events.onInputDown.add(function() {	figure1.animations.play('dosomething');}, this);figure1.events.onAnimationComplete.add(function(){	figure1.animations.play('stand');}, this);

So, this should play the "dosomething" animation (frame 1 to 6) when the sprite is being clicked. After the animations completes it should go back to the initial position, which is contained in frame 0 = "stand animation".


So here go the questions, is this the way to go? Do I have to create a "stand" pseudo animation? or is there some easier/better way to achieve this? (I am also just seeing, the stand animation would indefinitely fire the onAnimationsComplete event?!)


Kind regards,

Link to comment
Share on other sites

This forum is great :) Thanks for your answers, setting frame looks the way to go, but I have a strange problem, maybe you can see what mistake I am making?


I uploaded a demo here: http://www.steffmeister.at/dev/phaser/demos/frame.html


Clicking on the number should count to 5 twice, and then get back to 0, but it jumps back to 1, why is that so?


This is the code:

window.onload = function() {        var game = new Phaser.Game(400, 400, Phaser.AUTO, '', { preload: preload, create: create });	var framedemo;		        function preload () {			game.stage.backgroundColor = '#fff4d9';						game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;			game.scale.setScreenSize();            game.load.spritesheet('framedemo', 'framedemo.png', 264, 428, 6);        }		        function create () {            framedemo = game.add.sprite(10, 10, 'framedemo');            framedemo.animations.add('count', [1,2,3,4,5], 5, true);            framedemo.inputEnabled = true;            framedemo.events.onInputDown.add(function() {				console.log('pressed');				framedemo.animations.play('count');            }, this);            framedemo.events.onAnimationLoop.add(function(){				console.log(framedemo.animations.currentAnim.loopCount);				if (framedemo.animations.currentAnim.loopCount >= 2) {					framedemo.animations.stop();					framedemo.frame = 0;				} 			}, this);        }    };

and this is the spritesheet:framedemo.png


I removed everything unnecessary, but it always jumps back to 1 and not 0. Can you help me?


Kind regards,


Link to comment
Share on other sites

Thanks for your answer.


It's returning to the first frame of animation. Try replacing:


I also came to that conclusion but why? I understand stop() would do that, but why is the frame=0 line not produce anything? :(


framedemo.animations.stop();framedemo.frame = 0;
framedemo.animations.stop('count', 0);


I updated your demo with your suggestion but it has the same effect. :/

Link to comment
Share on other sites

So, your suggestion is wrong I guess, according to the documentation:


stop(name, resetFrame)
Stop playback of an animation. If a name is given that specific animation is stopped, otherwise the current animation is stopped. The currentAnim property of the AnimationManager is automatically set to the animation given.


So, the second parameter resetFrame would be the frame# of the mentioned animation.



What I tried now is playing around with setting frame, I put this in the oninput handler instead of the animation.play function:

framedemo.frame++;if (framedemo.frame >= 5) framedemo.frame = 0;

and this works as expected.


I wonder if playing an animation modifies this behaviour, so that setting frame 0 is impossible?!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...