(solved) Long click button | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

(solved) Long click button

I'm making a game with a big world, but clicking on the left and right buttons just moves the player some pixels to left or right. It's slow and boring. It would be nice if you could long click on the buttons, and move until you release the button. How can I do that? https://code.sololearn.com/WZfk7yErVKpa/?ref=app

6th Jul 2019, 7:24 PM
Nerderkips
Nerderkips - avatar
2 Answers
+ 5
You have to update your game all the time (with requestAnimationFrame for example), and use these event listeners: mousedown and mouseup. You can then check with the function if the button is pressed down (maybe with a boolean value), and make that value false when it's up. If you want it to make it run on phones, you can use the touchstart and touchend events
6th Jul 2019, 7:34 PM
Airree
Airree - avatar
+ 4
Airree Thanks!
6th Jul 2019, 8:11 PM
Nerderkips
Nerderkips - avatar