Sign in to follow this  
wombat

Tween "isRunning" only on first step of sequence

Recommended Posts

Hi, I want to check whether a sequence of tweens has completed. However, the 'isRunning' flag only indicates whether the first step is 'running'. During step 2-4 in the example below it's set to false. It will be set to true again on the first step if the sequence loops.  

 

Is this how it's supposed to work? Is there a simple way to check whether the entire (non-looping) sequence has completed? 

var game = new Phaser.Game(640, 480, Phaser.CANVAS, '');var tween;var thing;var main_state = {    create: function() {        thing = {x: 10, y: 100 };        tween = game.add.tween(thing);        tween.to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out);         tween.to({x: 200, y: 300 }, 1000);        tween.to({x: 200, y: 100 }, 1000);        tween.to({x: 10, y: 100 }, 1000);        // tween.loop();        tween.start();    },    render: function() {        game.debug.text('tween.isRunning: ' + tween.isRunning, thing.x, thing.y);    }};game.state.add('main', main_state);game.state.start('main');

Share this post


Link to post
Share on other sites

you are not saving the result of the tween back into tween, so it generates a new one.

 

You could either chain the `to`s

tween = game.add.tween(thing) //see there is no ;.to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out) //again no ;.to({x: 200, y: 300 }, 1000); //this 2 to's are chained

or save the result back in tween

tween = game.add.tween(thing);tween = tween.to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out);tween = tween.to({x: 200, y: 300 }, 1000);

Edit: some info on the subject http://www.programmerinterview.com/index.php/javascript/javascript-method-chaining/

Share this post


Link to post
Share on other sites

Thanks for your reply! Storing the return value in tween like above doesn't change anything, however. The behavior is the same (In Chrome and Firefox) with this code: 

 tween = game.add.tween(thing); tween = tween.to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out);  tween = tween.to({x: 200, y: 300 }, 1000); tween = tween.to({x: 200, y: 100 }, 1000); tween = tween.to({x: 10, y: 100 }, 1000);

Anyway, am I missing something here? As I understand it, the .to() method doesn't return a new anything, it returns the 'tween' instance on which it was called, for chaining. So the var tween is referncing the same instance all along. 

Share this post


Link to post
Share on other sites

You could try testing the last child of the tween:

tween = game.add.tween(thing).to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out).to({x: 200, y: 300 }, 1000).to({x: 200, y: 100 }, 1000).to({x: 10, y: 100 }, 1000);if (tween._lastChild.isRunning) {}

Or you can use the onComplete callback:

tween = game.add.tween(thing).to({x: 10, y: 300 }, 1000, Phaser.Easing.Bounce.Out).to({x: 200, y: 300 }, 1000).to({x: 200, y: 100 }, 1000).to({x: 10, y: 100 }, 1000);tween._lastChild.onComplete.add(tweenDone); //calls the tweenDone function when completed

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.