Forum Topic: Key Press /key Down? - As3

(181 views • 7 replies)

This topic is 1 page long.

<< < > >>
Questioning

thespectre1

Reply To Post Reply & Quote

Posted at: 6/22/09 08:34 AM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

var J:Number = 0;
var up:Boolean = false;
function checkUPKey(event:KeyboardEvent) {
	if (event.keyCode == 38) {
		up = true;
	}
}
function checkKeyUP (event:KeyboardEvent){
		if (event.keyCode == 38) {
		event.keyCode = 0;
		up = false;
	}
}

function huge_fucking_function(event):void {
	if (up) {
		J +=1;
		trace(J);
	}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkUPKey);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUP);
stage.addEventListener(Event.ENTER_FRAME, huge_fucking_function);

this is my code in AS3
Copy paste it into your flash and notice how it traces as long as you keep the UP key pressed, i don't want that, i want it to add and trace with every key press

bear in mind that the huge_fucking_function has all the other movement codes in it.


Sleeping

thespectre1

Reply To Post Reply & Quote

Posted at: 6/22/09 10:18 AM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

Two hours guys??
no forum's that slow.
if it's not possible, which i highly doubt, just say so


None

tomdeaap

Reply To Post Reply & Quote

Posted at: 6/22/09 10:47 AM

tomdeaap LIGHT LEVEL 12

Sign-Up: 03/27/09

Posts: 87

Do you add the eventlisteners inside of the builder function? Have you imported Keyboard?

"Insert deep, brain twisting sentence here"


None

thespectre1

Reply To Post Reply & Quote

Posted at: 6/22/09 10:51 AM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

At 6/22/09 10:47 AM, tomdeaap wrote: Do you add the eventlisteners inside of the builder function? Have you imported Keyboard?

I add my eventListeners at the end of the code (Unless needed otherwise).
No i have not, how is it important or useful?
And How do i imported keyboard?


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 6/22/09 11:05 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

You're getting a constant trace because you're checking it on the enterFrame. If you want the code to run on just the key press then you have to put the code on the onKeyDown function. For example if you put trace("keyDown"); under your up = true; code then it will only trace when the up key is pressed, and not the whole time that (up) is true.


None

henke37

Reply To Post Reply & Quote

Posted at: 6/22/09 01:07 PM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

Actually, due to well, some reason, the key down event will fire multiple times if the key is hold down.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

BlindsightGames

Reply To Post Reply & Quote

Posted at: 6/22/09 02:03 PM

BlindsightGames DARK LEVEL 02

Sign-Up: 03/19/09

Posts: 24

You need to have a few things going on here. You will need two key events and a variable for your key.

First one for when the key is pressed. in that function:
- Check to see if the key is already set to be down, if not continue...
- Set the key variable to down
- do your action ... tracing in this case

You also need a key up function. In this function you only need to set the key variable to up.

That way you will only get one trace each time the button is pressed.


None

thespectre1

Reply To Post Reply & Quote

Posted at: 6/22/09 05:31 PM

thespectre1 DARK LEVEL 16

Sign-Up: 05/11/07

Posts: 230

At 6/22/09 11:05 AM, LonLonRanch wrote: You're getting a constant trace because you're checking it on the enterFrame. If you want the code to run on just the key press then you have to put the code on the onKeyDown function. For example if you put trace("keyDown"); under your up = true; code then it will only trace when the up key is pressed, and not the whole time that (up) is true.

What none of you seem to understand is i already understand this code, and that i wrote it myself.
and actually using it for other keys, but in the UP key i just want the AS3 equivalent for the onKeyPress();
or w/e it was in AS2, don't really know.


All times are Eastern Standard Time (GMT -5) | Current Time: 08:16 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!