Should I use onDown here and if so, how?

Recommended Posts

In my game I want my character to instantly move to predetermined points at the press of the left and right arrow keys.  The player starts at x coordinate of 0.  Here is my current code:

var n = 0;
cursors = game.input.keyboard.createCursorKeys();

var coordinates = [{x: 0}, {x: 100}, {x: 200}, {x: 300}, {x: 400}];

if (cursors.left.isDown && player.position.x != 0)
        n -= 1;
        player.position.x = coordinates[n].x;
if (cursors.right.isDown && player.position.x != 400)
        n += 1;
        player.position.x = coordinates[n].x;

So this basically works, but the problem is it runs it for however long I have the key pushed down, and I want it to only run once per key stroke.  I read that onDown is what I'm looking for, but I'm not sure how to use that.  Can someone help me out or suggest a better way?

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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.