lastnightsparty

Tweening random values in a loop

Recommended Posts

Hi, I've been trying to tween my sprite to random x and y co-ordinates in a loop, but after the initial random values are set it just loops between them instead of generating new random values. Anyone know how to solve this problem??

 

this.add.tween(this.sprite).to( { x: this.rnd.realInRange(-100, 100) }, 1000, Phaser.Easing.Linear.None)
   .to( { y: this.rnd.realInRange(-100, 100) }, 1000, Phaser.Easing.Linear.None)
   .loop()
   .start();

Share this post


Link to post
Share on other sites

Hello, I've not tried it myself, but I believe that once the initial tween is created you won't get any new values assigned, it will just continue to loop with the values it got randomly the first time. If you want to continue having it randomly tween, you don't want to loop the tween itself, but create tweens over and over again with different values to get the affect you want. You could use a timer that fires a custom function to do what you are looking for.

Share this post


Link to post
Share on other sites

Thanks Orbital,

 

That's what I did. Wrapped it in a loop function:

 

loopTween: function(){
this.add.tween(this.sprite).to({ x: this.rnd.realInRange(-100, 100) }, 1000, Phaser.Easing.Quadratic.None)
   .to({ y: this.rnd.realInRange(-100, 100) }, 1000, Phaser.Easing.Quadratic.None)
   .start()
   .onComplete.add(this.loopTween, this);

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

  • Recently Browsing   0 members

    No registered users viewing this page.