00:00
00:00
Newgrounds Background Image Theme

Reisaki just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

removing holding key button

380 Views | 4 Replies
New Topic Respond to this Topic

removing holding key button 2014-10-27 09:10:34


i want to remove that if you hold a key "A" you cant hold it it will just do the action once im using

keyCodes for the keys but the problem is you can hold it but i want it so you cant hold the button just 1 time click.

its a fighting game so im stuck here.

Response to removing holding key button 2014-10-27 09:31:10


At 10/27/14 09:10 AM, AnouarMocro wrote: i want to remove that if you hold a key "A" you cant hold it it will just do the action once im using

keyCodes for the keys but the problem is you can hold it but i want it so you cant hold the button just 1 time click.

its a fighting game so im stuck here.

Rather than checking for KEY_DOWN events, why not check for KEY_UP events? You can't hold up a key; it fires only after you've pressed and released it, and you can get the keyCode just like you would a normal KEY_DOWN event.
Of course, there's the problem that if you hold down the key, nothing will happen unless you release it...


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to removing holding key button 2014-10-27 09:52:25


You will need to set some sort of global boolean state to true when the KEY_DOWN event is fired and set it to false when the KEY_UP event is fired, and then simply use an if-statement to check the state of said boolean.

I wrote a tutorial on the subject some time ago that goes into depth regarding how to accomplish that. MSGHero also wrote an AS3 class that does the same thing, but I have not personally used it so I cannot vouch for its efficacy (not that I have any reason to think it won't work just fine).

Response to removing holding key button 2014-10-27 10:42:13


At 10/27/14 09:52 AM, Diki wrote: You will need to set some sort of global boolean state to true when the KEY_DOWN event is fired and set it to false when the KEY_UP event is fired, and then simply use an if-statement to check the state of said boolean.

I wrote a tutorial on the subject some time ago that goes into depth regarding how to accomplish that. MSGHero also wrote an AS3 class that does the same thing, but I have not personally used it so I cannot vouch for its efficacy (not that I have any reason to think it won't work just fine).

Why not remove the event listener for checking keys, then add them back again in the key up event? Having a dictionary seems a bit complicated imo. Only limitation is that no keys can be held down. Like:

import flash.events.KeyboardEvent;

function keydown(evt:KeyboardEvent)
{
	trace(evt.keyCode, "is pressed")
	stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown)
	stage.addEventListener(KeyboardEvent.KEY_UP, keyup)
}
function keyup(evt:KeyboardEvent)
{
	trace(evt.keyCode, "is released")
	stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown)	
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown)

Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to removing holding key button 2014-10-27 10:56:11


At 10/27/14 10:42 AM, Gimmick wrote: Why not remove the event listener for checking keys, then add them back again in the key up event? Having a dictionary seems a bit complicated imo. Only limitation is that no keys can be held down. Like:

Like you said: that way cannot determine if a key is being held down, only that it was pressed. That way also cannot determine if multiple keys have been pressed. And, most importantly, that way is actually significantly more complicated: imagine the code required to have multiple objects/instances being altered by a single key (e.g. moving two different characters with a single key).

Adding and removing so many event listeners will probably be significantly slower, too.