Jump to content

How to stop player movement once they hit a specific coordinate?


MarrMooMoo
 Share

Recommended Posts

I'm still new to Phaser and coding in general so I apologize if this doesn't make sense.  I have player gravity and bounce set to 0 and movement velocity cranked up so movement seems instant.  I'm looking to make it so that once the player hits a certain point on the x axis it stops, then the movement key must be pressed again to continue in either direction until it hits another one of those points, and so on.

One idea I had was to populate the screen with vertical immovable bars with collision spread out evenly, but I don't know how to make it so that once the player hits them and stops their momentum they can then proceed through them at the press of the movement key (or if thats even possible).

I feel like its more likely that I'd be able to set a coordinate for movement to stop, but again I don't know if thats possible either.  Any help would be appreciated!

Link to comment
Share on other sites

You can add the points to an array and iterate trough the array in update. To make the player move again you can use input events:

var Xcoordinates=[100,200,340];
var offset=5;

var movementKey;

function create()
{
    movementKey=game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    movementKey.onDown.add(walk, game);
}

function update()
{
    Xcoordinates.forEach(function(xcoord)
    {
        if((player.x>(xcoord-offset))&&(player.x<xcoord+offset))
        {
          player.body.velocity.x=0;
        }
    };

}

function walk()
{
    player.body.velocity.x=200;
}

input event example:

For the vertical bars approach you can use arcade physics and resize the physics body using either sprite scaling or setSize. Then use game.physics.arcade.overlap() to check for overlaps. Again you can use input events to move the object once is has come to a stop.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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