At 11/4/09 04:56 PM, rmbrstrongbad18 wrote:
if(keyPress "<F>"){
if(Key.isDown(65)){
gotoAndPlay(6);
}
}
Ah, I see.
Right, I'm going to teach you how to do it on a movie clip, as that seems to be the kind of code that you're used to.
You're more than welcome to learn key listeners (and I recommend it), but don't get fooled into thinking that it is the best technique. Key listeners 'activate' every time any key is pressed and is independent of the frame rate. This can be great sometimes! But one big disadvantage is that it doesn't continue to perform a function if a key is held down (this property alone can't be used for your keyPress function as the code will still occur if they key is held down and any other key is pressed). Hence, you need need to pass values to your onEnterFrame or onClipEvent(enterFrame) code and by that point your code may have gotten unnecessarily bloated.
Basically, choose wisely.
Right, code (put this on a movie clip; you know what to do):
onClipEvent(load) { //activates only once, when the movie clip appears on the stage
//note, if you already have one of these on the movie clip, just put the below line of code in your other one
var fkeypressed:Boolean = false; //creates a variable we will use to hold the current state of the 'f' key
}
onClipEvent(enterFrame) { //activates once per frame
//again, if you already have one, just put the below code in that instead of making a new one
if(Key.isDown(70)) { //checks if the 'f' key (70) is being held down
if(fkeypressed == false) { //checks whether the 'f' key had already been pressed using the variable
fkeypressed = true; //if it hadn't already been pressed, change the variable to tell it that it has now been pressed
if(Key.isDown(65)) { //checks if the 'a' key (65) is being held down
trace("You are holding down the 'a' key and have pressed the 'f' key"); //this shows that it works; remove it once you are done testing
gotoAndPlay(6); //you know what this does
} //end of 'a' test
} //end of 'fkeypressed' test
} else { //executes only if the 'f' key is not being held down; that is to say that it is 'up'
fkeypressed = false; //changes the variable back to false so that the key can be pressed again
}
}
I've checked, and that works. Put it on a movie clip, hold down 'a', and hold down 'f', and the 'trace' code I put in there will only run once. Release 'f', press it again, and it traces again.
To make code that runs if 'a' is down only, either do it the simple way by putting that code separate to this, or mess about with the order of these 'if' statements (not recommended until you fully understand the concept).
Also note that if you very quickly tap the 'f' key, you may find that it won't register. That is because you held the key down for less than a frame, and so it didn't realise it was down at all. Such problems can be addressed by using listeners (or a greater frame rate).
I hope you can understand how that all works. If not, try using 'trace' statements and see what happens when you do different things.