MarrMooMoo

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

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!

Share this post


Link to post
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.

Share this post


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.