Jump to content

Tween Rotation loop


Tanikaze
 Share

Recommended Posts

Hi, I've an issue with the tweening system. I need a sprite that have to make a complete rotation each second, forever. To do it I've applied an appropriate tween and the set it loop property to true, but of course after the first iteration the sprite have the rotation value set in the tween, so it doesn't move.

I've tried to intercept the onStart event (for some reason, i cannot intercept the onLoop, but the onStart on the start of each loop) to manually set the rotation to 0 before the next loop, but after a couple of try I've understood that you cannot manually set a property used in an active tween. What I can do?

 

Edit: I've also tried to stop the tween, change the property and restart it, nothing. I could also try to destroy the tween itself, change rotation and create a new one, but it's too much expensive to do I fear

 

Here is my code:

MyGame.prototype = {  create: function() {    //sprites    this.objects['player'] = this.add.sprite(MyGame.GAME_WIDTH / 2, 500, 'player');        this.objects['player'].anchor.x += 0.5;        this.objects['player'].anchor.y += 0.5;    //tweens    this.tweens['playerRotation'] = this.add.tween (this.objects['player']);    this.tweens['playerRotation'].to ({rotation : 3.14}, 500)        .loop(true);    this.tweens['playerRotation'].onStart.add(this.loopListener, this);    this.tweens['playerRotation'].start ();  },  loopListener : function (sprite) {    sprite.rotation = 0;    //also tried this.objects['player'].rotation = 0; nothing  },  .  .  .}
Link to comment
Share on other sites

Loop should make the tween go back to its original value and start again - try this (using angle instead, which is easier to manage):

// Whacking it all into a single function keeps things tidy - the parameters for 'to' are properties, time, ease, auto start, delay and times to repeat (Infinity will keep it repeating forever) and yoyo (not used, makes the tween go backwards every other repeat) this.tweens['playerRotation'] = this.add.tween(this.objects['player'])  .to({angle: 359}, 500, null, true, 0, Infinity);

I would also strongly recommend you move away from the square bracket style of property lookup - it makes things very difficult to read and requires more typing than is necessary. You should be using dot notation for this stuff, like so:

this.tweens.playerRotation = this.add.tween(this.objects.player);

One of the only times square bracket notation is recommended is when you need to access dynamic properties, such as this.objects['player' + number] etc.

Link to comment
Share on other sites

Thanks, I will go to study well the full signature of the Tween.to() to full understand that line of code.

P.S.: sorry for the brackets, i forgot to delete them when cleaning my code to submission. I use it temporarily for a debug procedure, until I will find the time to write something that visit all the Stage element tree

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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