Forum Topic: All Keys Down

(110 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

kramlack

Reply To Post Reply & Quote

Posted at: 1/2/09 09:03 PM

kramlack EVIL LEVEL 08

Sign-Up: 02/18/06

Posts: 341

Hi, I have a two part question.

The first is, is there a command for having all keys down, for example, " Key.isDown('all keys command')". The reason is, I'm trying to make a command for 'if all the keys AREN'T down'. This may seem stupid, but the reason for this is because I want my HERO MC to play the stand animation (frame one inside the MC) if none of the keys are down, but for some reason, the way I have it set up doesn't seem to work, the code right now, is as follows:

onClipEvent(enterFrame) {
gotoAndStop(1);
if(Key.isDown(Key.RIGHT)) {
_x += 5;
gotoAndStop(2);
}
if(Key.isDown(Key.LEFT)) {
_x -= 5;
gotoAndStop(2);
}
}

He plays the stand when not moving, but, when he does move, for some reason it only plays the first frame of the walk animation. Lately, I've been trying a new way of coding more professionally than I used to, so this has been a great bother to me, especially when I feel the answer should be so simple to figure out.

Finally, I'd like to reiterate my question, can someone tell me if there's a command for all keys, or better yet, an easy method of fixing this problem. Any help is greatly appreciated!


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 1/2/09 09:31 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,833

hmm ive never tried this,you can use a loops to cycle through all the key codes.
try:

for(i=8;i<=191;i++){
if(!Key.isDown(i)){
trace("No keys are down")
}
}

that should work fine

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 1/2/09 09:34 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,833

fuck i need to read in further nextime, instead of 2 ifs, use an if and and else if and an else for the standing animation,use the help files.

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.


Winking

zuperxtreme

Reply To Post Reply & Quote

Posted at: 1/2/09 09:42 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,626

if(this key is down){
//do this
}else if(this other key is down){
//do this
}else{
//do this if the previous two aren't met
}


None

korded

Reply To Post Reply & Quote

Posted at: 1/2/09 09:55 PM

korded EVIL LEVEL 06

Sign-Up: 02/06/00

Posts: 1,094

Maybe I'm just out of it right now, but I think at least one big problem there is you have your key checks all within the onEnterFrame handler.. as well as the line of code before them saying to go to frame 1. So, every time a frame is played, the timeline is sent to frame 1, and then if one of the keys is down, is sent to frame 2.. but then back to frame 1 again right after, then frame 2, and so on.

Add the else { gotoAndStop(1) like the guy above said instead.


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 1/2/09 10:04 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,833

At 1/2/09 09:55 PM, korded wrote: Maybe I'm just out of it right now

yup,must be. crack is whack.

think about it.

Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.


None

kramlack

Reply To Post Reply & Quote

Posted at: 1/2/09 11:56 PM

kramlack EVIL LEVEL 08

Sign-Up: 02/18/06

Posts: 341

At 1/2/09 09:55 PM, korded wrote: Maybe I'm just out of it right now, but I think at least one big problem there is you have your key checks all within the onEnterFrame handler.. as well as the line of code before them saying to go to frame 1. So, every time a frame is played, the timeline is sent to frame 1, and then if one of the keys is down, is sent to frame 2.. but then back to frame 1 again right after, then frame 2, and so on.

Add the else { gotoAndStop(1) like the guy above said instead.

I'd like to, since that was my old method of programming, but I always assumed it was an 'incorrect' method, continually using else if statements for something as simple as key presses. Is there no other way around it?


None

korded

Reply To Post Reply & Quote

Posted at: 1/3/09 12:55 AM

korded EVIL LEVEL 06

Sign-Up: 02/06/00

Posts: 1,094

I don't think it's that big a deal when used on such a small scale as what you've shown above.. the only real problem with it was it was constantly setting your timeline to the first frame, then toggling between the first and second frame really fast when you have a key down.

...but yes, in the case of key pressed at least, there is an alternative fortunately. Check out listeners. You'll have to look them up (actionscript dictionary?) for real understanding, but here's an example.

keytracker = new Object();

keytracker.onKeyDown = function() {
	if (Key.getCode() == 65) {
		trace("you are pressing down A");
	} else if (Key.getCode() == 68){
                trace("you are pressing down D");
       }
}

keytracker.onKeyUp = function() {
	if (Key.getCode() == 65) {
		trace("you have released A");
	} else if (Key.getCode() == 68) {
		trace("you have released D");
	}
}

Key.addListener(keytracker);

None

korded

Reply To Post Reply & Quote

Posted at: 1/3/09 01:01 AM

korded EVIL LEVEL 06

Sign-Up: 02/06/00

Posts: 1,094

One point I should mention actually is that using the above method, your script isn't constantly checking for a condition; the listener will perform the specified actions one time upon your trigger.. which in the above case is the pressing and releasing of the A and D keys.

Someone more experienced can correct me if I'm off on something here.


None

kramlack

Reply To Post Reply & Quote

Posted at: 1/3/09 01:31 AM

kramlack EVIL LEVEL 08

Sign-Up: 02/18/06

Posts: 341

Thanks, sounds fairly understandable. I'll make sure to look into listeners for further explanation.


None

faxmunky

Reply To Post Reply & Quote

Posted at: 1/3/09 01:36 AM

faxmunky DARK LEVEL 04

Sign-Up: 12/03/08

Posts: 301

onClipEvent(key.UP){}

I think that's right... not sure though

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 01:14 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!