View Full Version : Counting variable by keypress
So im making this platform game. And i want a double jump function.
so i figured a way to do that. make a variable "jumpCounter", and each time spacebar is pressed 1 is added to the jumpcounter, and each time the character hits the floor, the counter is reset.
here's the code:
if (Key.isDown(Key.SPACE) && jumpcounter<=2) {
grav = maxJump;
jumpcounter += 1;
}
my problem being:
it registers that spacebar is held in for to long, and the counter adds 2-5.
how to i tell flash to ONLY add 1?
In advance, thanks!
every answer is appreciated
Schmoopy
03-09-2009, 04:33 PM
I know nothing about flash, but just from general knowledge, maybe add the counter on Key.isUp? But still have the jump on key down, then it would only increment the counter once. Just see if it works, I could be wrong :p
[edit]
Maybe even on Key.onPress, if that's an event you can use, then it will wait for the key to be depressed and released again.
that's a good way to do it, but when i start to type in the code, only isToggled, and isDown is comming up as options, i dont think that flash offers that option, something that would be really wierd :S
Schmoopy
03-09-2009, 04:41 PM
that's a good way to do it, but when i start to type in the code, only isToggled, and isDown is comming up as options, i dont think that flash offers that option, something that would be really wierd :S
That's a shame :(
But I found something through google:
on(keyPress "<left>") {
nextframe();
}
[edit]
My attempt...
on(keyPress "KEY.SPACE")
{
if(jumpcounter <= 2)
{
grav = maxJump;
jumpcounter += 1;
}
}
Something like that, I don't really expect it to work as it is but since you know what the syntax is I'm sure you could mould it somehow.
exact same response as the last. doesn't count only one time.
but thank you very much for trying! really
Schmoopy
03-09-2009, 04:53 PM
Which version of flash are you using? If you have Flash MX 2004 there is a function for keyUp : http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00003822.html
Also found this code:
var myListener = new Object();
myListener.onKeyDown = function() {
// trace ("You pressed a key.");
// trace("DOWN -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
};
myListener.onKeyUp = function() {
trace("You released a key.");
trace("UP -> Code: " + Key.getCode() + "\tACSII: " + Key.getAscii() + "\tKey: " + chr(Key.getAscii()));
};
Key.addListener(myListener);
Maybe try something like that :)
i really cant believe that's nescesary, all i need is for the keypress to only add 1 to my variable. the code you wrote there is way more complex than the code i have so far, and it cant be nescesary, can it?
im using flash 8. actionscript 2.
the KeyUp is for buttons.
on(release){
action
}
Schmoopy
03-09-2009, 05:02 PM
Hmm... Wait for Medyman to get here ;)
Okay, but thank you so much for you effort. srsly!
Medyman
03-09-2009, 08:19 PM
Elan,
You mentioned you're using the following code:
if (Key.isDown(Key.SPACE) && jumpcounter<=2) {
grav = maxJump;
jumpcounter += 1;
}
Is that standalone or part of a larger function? In theory, that's correct. But, it has to be implemented properly. My guess is that you're adding that function within an onEnterFrame event or a function that's called in rapid succession.
A solution would be (as Schmoopy suggested) would be to use an event listener. Listen for the onKeyDown event, then test to see if the pressed key is the space bar. If it is, adjust your variables accordingly.
the KeyUp is for buttons.
onKeyUp() is an event. You can attach any function to it that you want. It doesn't need to be a button.
For example:
MovieClip.onKeyUp = function () {
trace ("You pressed a key");
};
You can use the Key.getAscii and Key.getCode methods to determine which key was pressed
Okay i think i understand, thank you.
MovieClip.onKeyUp = function () {
trace ("You pressed a key");
};
how to i tell flash that i mean the button space?
im not familiar with Key.getCode or Key.getAscii yet.
Medyman
03-09-2009, 08:37 PM
It's fairly straightforward. You could either of those methods within the onKeyUp (or onKeyDown) function to determine the key that is pressed.
For example:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
trace("You just typed: "+Key.getCode());
};
Key.addListener(keyListener);
Key.getAscii() and Key.getCode() are pretty much the same. The main difference is that Key.getAscii() respects the key's capitalization and Key.getCode() does not.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.