Daniel Wilkes
Daniel Wilkes

Reputation: 79

How do you rotate the 'player' in a Phaser game?

I am developing an arena style zombie game. I am in very early stages and I can't find a way to rotate the player the way I want.

I want to be able to rotate the player using left-arrow and right-arrow and move forward with up-arrow.

This is my current player movement conditions:

 if(this.cursor.left.isDown){
        this.player.angle += -0.5;
    }
    else if(this.cursor.right.isDown){
        this.player.angle += 0.5;
    }
    else if(this.cursor.up.isDown){
        this.player.body.velocity.y = -75;
    }
    else{
        this.player.body.velocity.x = 0;
        this.player.body.velocity.y = 0;
    }

The problem with this approach is that, although I can change the rotation of the player with .angle, when I move forward it still just moves me 'up' the screen instead of 'forward' compared to my rotation.

Sorry if I have explained poorly.

Upvotes: 1

Views: 3467

Answers (1)

Shohanur Rahaman
Shohanur Rahaman

Reputation: 387

I cant understand your question properly, but I think this code may be help you.you can use left and right key to rotate the car sprite and up key to move foreword.

    function preload(){
    game.load.image('car', 'assets/sprites/car90.png');
}

var car ;

function create(){
    game.physics.startSystem(Phaser.Physics.ARCADE);
        car = game.add.sprite(game.world.centerX,game.world.centerY,'car');
        car.anchor.setTo(0.5,0.5);
        game.physics.arcade.enable(game.global.car);
        car.body.allowRotation = true;
}

function update(){

            if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){
                game.global.car.body.angularVelocity = -200;
            }
            else if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
                game.global.car.body.angularVelocity = 200;
            }
            else if(game.input.keyboard.isDown(Phaser.Keyboard.UP)){
               game.global.car.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(game.global.car.angle, 200));
            }
            else{
                game.global.car.body.angularVelocity = 0;
                game.global.car.body.velocity.x = 0;
                game.global.car.body.velocity.y = 0;
            }
}

Upvotes: 2

Related Questions