Jump to content

Player Can't Move: Up and Down


fedora
 Share

Recommended Posts

I am building a game using a Tile Map.  The Map loads and all appears fine.  I can move the player right and left with no issues. Unfortunately, I have been unable to get movement up and down.  I have scoured web with no success.  I am not looking for 'Pac Man" auto movement.  I want the player to move on keyboard input.  Here is the relevant sections of code:

this.physics.arcade.gravity.y = 200;    // The player and its settings    player = this.add.sprite(200, this.world.height - 250, 'dave');    //  We need to enable physics on the player    this.physics.arcade.enable(player);    //  Player physics properties.    player.body.bounce.y = 0.2;    player.body.gravity.y = 300;    player.body.collideWorldBounds = true;        //  Our animations of walking    player.animations.add('left', [0, 1, 2, 3], 10, true);    player.animations.add('right', [5, 6, 7, 8], 10, true);    player.animations.add('up', [0, 1, 2, 3], 10, true);    player.animations.add('down', [5, 6, 7, 8], 10, true);    
    //  Keyboard Controls          cursors = this.input.keyboard.createCursorKeys();
//  Reset the players velocity (movement)    player.body.velocity.x = 0;    if (cursors.left.isDown)    {        //  Move to the left        player.body.velocity.x = -150;        player.animations.play('left');    }    else if (cursors.right.isDown)    {        //  Move to the right        player.body.velocity.x = 150;        player.animations.play('right');    }            else if (cursors.up.isDown)    {        //  Move to the Up        player.body.velocity.y = 150;        player.animations.play('up');    }            else if (cursors.down.isDown)    {        //  Move to the Down        player.body.velocity.y = 150;        player.animations.play('down');    }

Any guidance would be greatly appreciated.

Link to comment
Share on other sites

You should try putting the X and Y movement in their own if statements; i.e. don't do if(left) else if (right) *else* if (up)... separate those bad boys.

 

Try getting rid of gravity entirely, as well, see if you get movement up (after you fix the 150/-150 issue from the above post).

Link to comment
Share on other sites

Thank you both for your replies.  

 

I have resolved the issue by making the below changes:

    //this.physics.arcade.gravity.x = 200;    //  Player physics properties. Give the little guy a slight bounce.    //player.body.bounce.y = 0.2;    //player.body.gravity.y = 300;    //player.body.collideWorldBounds = true;
        else if (cursors.up.isDown)    {        //  Move to the Up        player.body.velocity.y = -150;        player.animations.play('up');    }            else if (cursors.down.isDown)    {        //  Move to the Down        player.body.velocity.y = 150;        player.animations.play('down');    }

Per your suggestion, drhayes -- I will also be breaking out x and y movement into their own if statements

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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