Jump to content

Rotate back and forth using tween


jung34
 Share

Recommended Posts

Hi, 

 

Is there a way to tween a rotation between two angles (say -5 to 5 degrees)?

 

I can tween to a single angle and then yoyo; but I'm not sure how to get it to tween between two angles so it rocks back and forth.

 

If possible, I'd prefer to have a single tween rather than two; however if that's the only way, then I'd still be interested to know how to get that to work so that it loops.

 

 

 

Link to comment
Share on other sites

Hi, you can write your custom easing function:

 

Here is working example:

            var s = new Phaser.Sprite(this.game, this.world.centerX, this.world.centerY, "Game", "VerticalCannonSprite");            s.anchor.setTo(0.5, 1);            this.world.add(s);            this.add.tween(s).to({ angle: 45 }, 2000, function(k) {                return Math.sin(Math.PI * 2 * k);            }, true, 0, -1);

Just return some value for input (k) from 0 to 1. The angle value 45 degrees then works like amplitude.

 

I wrote article about custom tweens even with parameters on my blog: http://sbcgamesdev.blogspot.cz/2015/04/phaser-tutorial-custom-easing-functions.html

It is also used for enemy drones movement in this simple game tutorial: http://sbcgamesdev.blogspot.cz/2015/05/phaser-tutorial-dronshooter-simple-game.html

Link to comment
Share on other sites

function tweenRotation(object) {    var randomDelay = rData.between(0, 3000);    var amountRotation = 5;    object.angle = amountRotation * -1;    object.anchor.set(.5);    var move = game.add.tween(object).to({        angle: amountRotation,    }, 3500, Phaser.Easing.Quadratic.InOut, true, randomDelay, -1, true);   }

rData is phasers random data generator. 

 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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