Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Counting variable by keypress

  1. #1
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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

    [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.
    Last edited by Schmoopy; 03-09-2009 at 04:39 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Quote Originally Posted by Elan View Post
    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    exact same response as the last. doesn't count only one time.
    but thank you very much for trying! really

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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/...=00003822.html

    Also found this code:

    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

  7. #7
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    }

  8. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Hmm... Wait for Medyman to get here

  9. #9
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Okay, but thank you so much for you effort. srsly!

  10. #10
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Elan,

    You mentioned you're using the following code:
    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:
    Code:
    MovieClip.onKeyUp = function () {
    	trace ("You pressed a key");
    };
    You can use the Key.getAscii and Key.getCode methods to determine which key was pressed

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •