A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Key is released

  1. #1
    Impressive Click swcAndrew's Avatar
    Join Date
    Oct 2004
    Location
    USA
    Posts
    468

    Key is released

    Just wondering if there is a way to code when a key is released. I am not talking about mouse release but more of arrow keys or even any key release. What would it look like?
    Andrew Webster
    Impressive Click

  2. #2
    something like this
    code:

    if(key.isDown(key.space) {
    do stuff;
    }


  3. #3
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    onClipEvent (keyUp) {
    }

    might also help
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  4. #4
    Impressive Click swcAndrew's Avatar
    Join Date
    Oct 2004
    Location
    USA
    Posts
    468
    Originally posted by clinton2
    something like this
    code:

    if(key.isDown(key.space) {
    do stuff;
    }

    No I am not talking about pressing keys, because I know how to do that. Maybe an example might help:

    ex:
    when the space bar is pressed than nothing happens. But when you release your character shoots his gun.

    The reason for this question was when I was thinking about a way so that the player can't hold onto a key. Instead he would constantly have to keep pressing it, for whatever reason.
    Andrew Webster
    Impressive Click

  5. #5
    Senior Member dogtown08's Avatar
    Join Date
    Jul 2004
    Location
    In a later dimension
    Posts
    201
    Thats odd, i dont know of any other way than something like this:
    code:
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.SPACE)) {
    down = true;
    }
    }
    onClipEvent (keyUp) {
    if (down == true) {
    //put all your actions here
    }
    }



    Maybe there is a better way, because I could see how that might eventually become slow and confusing.

  6. #6
    Senior Member
    Join Date
    Feb 2004
    Location
    Australia
    Posts
    156
    umm key listeners should do stuff on key up, but i've never been able to use them, to do what i want.........

    code:
    k = new Object();
    k.onKeyUp = function() {
    k_rc();
    };
    Key.addListener(k);



    but i'm not sure how to get it to work for a specific key (such as space)
    Last edited by Dr Warm; 11-16-2004 at 09:15 PM.

  7. #7
    Impressive Click swcAndrew's Avatar
    Join Date
    Oct 2004
    Location
    USA
    Posts
    468
    I am confused
    k.onKeyUp = function() {
    k_rc();
    }
    how can you have keyUp when k is an object? Or is the key "k" being assigned a value? Sorry but can someone explain.
    Andrew Webster
    Impressive Click

  8. #8
    Senior Member
    Join Date
    Aug 2004
    Location
    San Diego, California
    Posts
    421
    You are right Dr Warm. This is how you get it to work for the space bar.

    code:

    k = new Object();
    k.onKeyUp = function() {
    //here 32 is the key code for space bar
    if (Key.getCode() == 32) {
    //fire,shoot,etc
    }
    }
    Key.addListener(k);


  9. #9
    Zakarus's Tale Developer Zakarus2001's Avatar
    Join Date
    May 2001
    Location
    Here, lurking
    Posts
    237
    Code:
    k = new Object();
    k.spacePressed = false;
    k.onKeyDown = function()
    {
      if ( Key.isDown( Key.SPACE ) ) this.spacePressed = true;
      else this.spacePressed = false;
    }
    k.onKeyUp = function()
    {
      if ( k.spacePressed )
      {
         // code
      }
    
    }
    Key.addListener( k );
    What that does:
    It creates an object, k. The way an object works is it creates a variable that can have methods and properties. Properties are like the _x and _y of a movieclip. Methods are the functions the object can do. Like you can do movieClip.hitTest( stuff ). That is a method of the MovieClip object( sortof ). So this code basically assigns two methods and one property to the object k. The property basically is just a boolean value that says whether or not the space was pressed. The two methods are onKeyUp and onKeyDown, these methods know when to be called as that is coded into the compiler. We are just telling the compiler what to do when these events happen. The last line just tells the Key object that we want to give it a new listener.

    Cheers,
    ~Zakarus2001
    "Come human relieve yourself of the ignorance society has placed on your head" ~Zakarus 24:7

  10. #10
    Senior Member
    Join Date
    Aug 2004
    Location
    San Diego, California
    Posts
    421
    Hey, Zakarus2001, why the heck would you want to go through the trouble of using 'if' statements, variables, and the onKeyDown method when the swcAndrew just wants to check when the space bar is let go and not pressed? I'm confused. Is there an advantage to using a the spacePressed variable?

  11. #11
    Zakarus's Tale Developer Zakarus2001's Avatar
    Join Date
    May 2001
    Location
    Here, lurking
    Posts
    237
    *bows head in shame*

    I posted that about the same time you did, so I didn't read your post before I posted( as it wasn't there yet. )

    I've been using Flash since 4 and I had no idea there even WAS a Key.getCode method .

    So no, andross_88, there is no befefit AT ALL to what I did, and your way is much better and cleaner. I've just never thought about it that much.

    Thanks though, I learned something new about the Key object.

    Cheers,
    ~Zakarus2001
    "Come human relieve yourself of the ignorance society has placed on your head" ~Zakarus 24:7

  12. #12
    Senior Member
    Join Date
    Aug 2004
    Location
    San Diego, California
    Posts
    421
    Its okay man, no need to be ashamed.

    At least you got something out of this post.

  13. #13
    Impressive Click swcAndrew's Avatar
    Join Date
    Oct 2004
    Location
    USA
    Posts
    468
    just to clarify is:
    code:

    if (Key.getCode() == 32) {
    //fire,shoot,etc
    }


    this is space bar right because I have never worked with getCode before.
    Andrew Webster
    Impressive Click

  14. #14
    Senior Member
    Join Date
    Feb 2004
    Location
    Australia
    Posts
    156
    this is space bar right because I have never worked with getCode before.
    yep, of course you could always test it!

    Thnx for the info ppl had always told me the way like Zakarus2001 did but i new there had to be a simpler way, my problem was using Key.isDown instead of Key.getCode() yay, i'm happy!!!

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center