Jump to content

Change angle for movement but not sprite?


xronn
 Share

Recommended Posts

Hi,

My sprites direction of travel is controlled by the angle in which he is facing so he basically only gos up but the angle makes him move left and right.

 

    if (this.cursor.left)    {    	this.tank.angle = 180;        this.currentSpeed = 80;        this.tank.animations.play('left', 7, true);    }    else if (this.cursor.right)    {    	this.tank.angle = 360;        this.currentSpeed = 80;        this.tank.animations.play('right', 7, true);    }	    if (this.cursor.up)    {    	this.tank.angle = 270;        this.currentSpeed = 80;        this.tank.animations.play('up', 7, true);    }    else    {        if (this.currentSpeed > 0)        {            this.currentSpeed -= 1;        } else {        	this.tank.animations.play('standing', 7, true);        }    }    if (this.currentSpeed > 0)    {        game.physics.arcade.velocityFromRotation(this.tank.rotation, this.currentSpeed, this.tank.body.velocity);    }        else    {        game.physics.arcade.velocityFromRotation(this.tank.rotation, 0, this.tank.body.velocity);    }

The only problem is the sprites orientation changes based on the angle, of course it would you've just told it to..

Without having to flip my hard assets so they appear to be facing the right way (not upside down) I was wondering if phaser had a similar function to the immovable but for rotation!


Thanks!

Link to comment
Share on other sites

So to move my sprite left and right force the angle of the sprite to change e.g

this.tank.angle = 360;

Setting the player sprite at 360 degrees, when you press this key it now moves to the right as that's the way the sprite is facing.

but forcing an angle on the sprite also rotates the image as your setting it at an angle, I want to angle in the sprite in order to move it but not actually rotate the sprite.

Link to comment
Share on other sites

You want to move a sprite to any direction, and you are using angle to achieve that. But angle causes sprite to rotate and you don't want that.

 

Well don't use angle because that is for actually rotating the sprite. There are many other alternative methods to move a sprite to any direction.

 

http://docs.phaser.io/Phaser.Physics.Arcade.html#accelerateToXY

http://docs.phaser.io/Phaser.Physics.Arcade.html#moveToXY

 

But actually to solve your problem you can still use velocityFromRotation like this:

// remove this, don't set angle on the sprite// this.tank.angle = 360;// Use any angle you want, just don't use sprite's rotationvar anyAngle = 270;game.physics.arcade.velocityFromAngle(anyAngle, this.currentSpeed, this.tank.body.velocity);

Note that there are two similar methods:

velocityFromRotation, is passed angle in radian, as in PI.

velocityFromAngle, is passed angle in degrees, as in 360.

 

So you should use the velocityFromAngle for degrees.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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