Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Should I use onDown here and if so, how?


MarrMooMoo
 Share

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?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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