# Simple yet weird rotation problem

## Recommended Posts

Hey guys, I'm newbie of Phaser so I hope you don't get mad for my simple problem.

My code simply moves a tank sprite around the screen, along with rotating its barrel (its childSprite) by using the mouse position...that's it.

The problem is the barrel: its rotation is completely broken! I used angleToPointer() directly from Phaser.io documentation but I can't find the solution of this problem.

video: broken_rotation.wmv

``````var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example',

var player;
var barrel;

}

function create() {
player.scale.setTo(0.5, 0.5);
player.anchor.setTo(0.5, 0.5);
barrel = new Phaser.Sprite(this.game, 0, 0, "barrel");
barrel.anchor.setTo(0,0.5)

}

function update() {

if (game.input.keyboard.isDown(Phaser.KeyCode.W))
{
speed = 3
var velocityX = Math.cos(player.angle * Math.PI / 180) * speed;
var velocityY = Math.sin(player.angle * Math.PI / 180) * speed;
player.x += velocityX;
player.y += velocityY;
}

if (game.input.keyboard.isDown(Phaser.KeyCode.D))
{
player.angle += 1;
}

if (game.input.keyboard.isDown(Phaser.KeyCode.A))
{
player.angle -= 1;
}

if (game.input.keyboard.isDown(Phaser.KeyCode.S))
{
speed = -2;
var velocityX = Math.cos(player.angle * Math.PI / 180) * speed;
var velocityY = Math.sin(player.angle * Math.PI / 180) * speed;
player.x += velocityX;
player.y += velocityY;
}

//barrel.angle += 1   the automatic rotation works perfectly!...

}

function render() {
game.debug.spriteInfo(barrel, 20, 32);

}
``````

##### Share on other sites

Rotating the player also rotates its child. Try

``````barrel.rotation  = game.physics.arcade.angleToPointer(barrel);
barrel.rotation -= player.rotation;``````