MarrMooMoo Posted September 18, 2017 Share Posted September 18, 2017 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 More sharing options...
samid737 Posted September 18, 2017 Share Posted September 18, 2017 You can add the onDown input event in create to run once:https://codepen.io/Samid737/pen/aLdpqa Jem Kazama and MarrMooMoo 2 Link to comment Share on other sites More sharing options...
MarrMooMoo Posted September 18, 2017 Author Share Posted September 18, 2017 16 minutes ago, samid737 said: You can add the onDown input event in create to run once:https://codepen.io/Samid737/pen/aLdpqa You are a saint. Thanks! samid737 1 Link to comment Share on other sites More sharing options...
Recommended Posts