This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about KeyEvent handlers when holding down the key  RSS feed

 
Pedro Pelizzaro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, people;

I have a boolean called grounded, that tells if the player character is touching the ground. It's set to true whenever the ground is touched, and false whenever the jump button is pressed;
My issue regards to the jump button. It's activated on this little segment of code:


Grounded is used to make sure double jumping won't happend, and to trigger gravity (wich increases the yv(y's speed) according to time). It seems to work fine if I hold down the button, but if I just tap it the jump will be many times smaller.
I am concluding that it means the if block is being executed multiple times, but that shouldn't happend since I set grounded to false right after it. When I hold the key, doesn't all of the code for it gets executed before it starts to loop? It appears as if it gets stuck on the if block for few seconds and then realizes that it should stop.
Is there something to the nature of Event Listeners that i'm not understanding? Why doesn't it do a single increase at yv and then stop no matter what?
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably also want to check that the event is KEY_RELEASED.
 
Pedro Pelizzaro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, before I started with gravity and platformer like stuff, I made it so the sprite could fly around all of the screen. And I've set it so when the key is released, the speed is set to 0, lol.

How silly of me. Thanks for making me look there again, I thought the problem was somewhere else. That's the problem of reusing code, I guess.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!