# Rotate back and forth using tween

## 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.

#### Share this post

##### Share on other sites

You can use repeat to keep yoyoing. You only need a second tween to get to your start rotation.

#### Share this post

##### 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

#### Share this post

##### 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.

#### Share this post

##### Share on other sites

Thank you Tom, feudalwars,

I learned something from both your posts.

I've used Tom's approach as it was the easiest to update my code

Have a great day!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×

• ### Recently Browsing   0 members

No registered users viewing this page.