Jump to content

# 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',
{ preload: preload, create: create, update: update, render: render })

var player;
var barrel;

function preload() {
game.load.image('tank', 'assets/PNG/Tanks/tankBlue_outline.png');
game.load.image("barrel", "assets/PNG/Tanks/barrelBlue_outline.png");
}

function create() {
player = game.add.sprite(200, 200, 'tank');
player.scale.setTo(0.5, 0.5);
player.anchor.setTo(0.5, 0.5);
barrel = new Phaser.Sprite(this.game, 0, 0, "barrel");
player.addChild(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!...
barrel.rotation = game.physics.arcade.angleToPointer(barrel);   // ...but not the input one sadly

}

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;

• ### Recently Browsing   0 members

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