Jump to content

Recommended Posts

I want multiple keys at once.

If I use D + shift, the player will stop, how to fix it?

SHIFT is to sprint the player and D is to walk the player on right side.

This is my code:

(game.input.keyboard.isDown(Phaser.Keyboard.A))
    {
        if(carDriveV != 0) 
        {
            car_1.body.velocity.x = -50;
            car_1.animations.play('c_drive');
        }
        else if (boatDrive != 0) {
            boat.body.velocity.x = -50;
            facing = 'left';
            boat.animations.play('boat_move_l');
        }
        else
        {
            player.body.velocity.x = -50;
            if (facing != 'left')
            {
            player.animations.play('left');
            facing = 'left';
            }
        }
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
    {
        if(carDriveV != 0) 
        {
            car_1.body.velocity.x = 200;
            car_1.animations.play('c_drive');
        }
        else if (boatDrive != 0) {
            boat.body.velocity.x = 50;
            facing = 'right';
            boat.animations.play('boat_move_r');
        }
        else 
        {
            player.body.velocity.x = 50;
		
		    if (facing != 'right')
            {
            player.animations.play('right');
            facing = 'right';
            }
        }
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.SHIFT)) {
        if(carDriveV != 0) 
        {
        carDriveV = 0;
        player.loadTexture('player');
        game.camera.follow(player);
        player.body.x = car_1.body.x+130;
        }
        else if (boatDrive != 0) {

        }
        else
        {
            if (facing == 'left')
            {
            player.animations.play('sprint_left');
            facing = 'left';
            player.body.velocity.x = -150;
            } 
            else 
            {
            player.animations.play('sprint_right');
            facing = 'right';
            player.body.velocity.x = 150;
            }
        }
    }

 

Link to post
Share on other sites

The problem is you're exclusively testing for keypresses (i.e. you're using else instead of a load of consecutive if statements). What you need to do instead is check for the keypresses separately and not exclusively to one another, and then do something based on which combination of keys has been detected as down on that frame.

This isn't exactly going to drop into your code above, but it gives you some idea of a possible approach:

// just a temporary velocity value we can use to apply to the body later
var velocity = 0;

// using else here is fine, as you likely won't handle a state where both directions are down
if (game.input.keyboard.isDown(Phaser.Keyboard.A) {
  // A key is down (left) so set our velocity to a negative number
  velocity = -50;
}
else if ((game.input.keyboard.isDown(Phaser.Keyboard.D) {
  // D key is down (right) so set our velocity to a positive number
  velocity = 50;
}

// notice this is not part of an else, this can be detected in addition to the previous
if ((game.input.keyboard.isDown(Phaser.Keyboard.SHIFT) {
  // shift key is down (speed) so double our velocity value
  velocity = velocity * 2;
}

// now we can apply the correct velocity here
player.body.velocity.x = velocity;

 

Link to post
Share on other sites

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.

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

×
×
  • Create New...