Key events class...
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Key events class...

  1. #1
    Member
    Join Date
    Sep 2004
    Location
    coventry,england
    Posts
    65

    Key events class...

    Hi,

    I've got a problem in a game i'm making with jumping. I'm trying to implement a double jump, and I think I need a way of checking if a key has been pressed, but that doesnt continue checking if the key is being held.

    I found a nice class on the web to replace the class we lost with AS3, which is below. using it, i can do:

    if(Key.isDown(Keyboard.UP)){ //code }

    and it works really well. however for the jump, i need a method inside the key class that just returns true the moment the key is pressed, and then goes back to false.

    I think that's what i need, anyway.. if anyone has any suggestions as to how to implement as double jump, i'd love to hear them too!

    the code's below

    thanks for any help!


    Code:
    // Key.as
    package {
        
        import flash.display.Stage;
        import flash.events.Event;
        import flash.events.KeyboardEvent;
        
        /*
         * The Key class recreates functionality of
         * Key.isDown of ActionScript 1 and 2. Before using
         * Key.isDown, you first need to initialize the
         * Key class with a reference to the stage using
         * its Key.initialize() method. For key
         * codes use the flash.ui.Keyboard class.
         *
         * Usage:
         * Key.initialize(stage);
         * if (Key.isDown(Keyboard.LEFT)) {
         *    // Left key is being pressed
         * }
         */
        public class Key 
    	{
            
            private static var initialized:Boolean = false;  // marks whether or not the class has been initialized
            private static var keysDown:Object = new Object();  // stores key codes of all keys pressed
            
            /*
             * Initializes the key class creating assigning event
             * handlers to capture necessary key events from the stage
             */
            public static function initialize(stage:Stage) 
    		{
                if (!initialized) 
    			{
                    // assign listeners for key presses and deactivation of the player
                    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                    stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
                    stage.addEventListener(Event.DEACTIVATE, clearKeys);
                    
                    // mark initialization as true so redundant
                    // calls do not reassign the event handlers
                    initialized = true;
                }
            }
            
            /*
             * Returns true or false if the key represented by the
             * keyCode passed is being pressed
             */
            public static function isDown(keyCode:uint):Boolean 
    		{
                if (!initialized) 
    			{
                    // throw an error if isDown is used
                    // prior to Key class initialization
                    throw new Error("Key class has yet been initialized.");
                }
                return Boolean(keyCode in keysDown);
            }
    	
    
    
    	
            /**
             * Event handler for capturing keys being pressed
             */
            private static function keyPressed(event:KeyboardEvent):void 
    		{
                // create a property in keysDown with the name of the keyCode
                keysDown[event.keyCode] = true;
    			
            }
            
            /**
             * Event handler for capturing keys being released
             */
            private static function keyReleased(event:KeyboardEvent):void 
    		{
                if (event.keyCode in keysDown) 
    			{
                    // delete the property in keysDown if it exists
                    delete keysDown[event.keyCode];
                }
            }
            
            /**
             * Event handler for Flash Player deactivation
             */
            private static function clearKeys(event:Event):void 
    		{
                // clear all keys in keysDown since the player cannot
                // detect keys being pressed or released when not focused
                keysDown = new Object();
            }
        }
    }
    'flash is da bomb'

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,436
    I move this to games.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    I wouldn't put that in the key class, I'd just use a variable or something that you set when the jumpkey is pressed, and set to false when it's not pressed. Only jump or double jump when this variable is false.

    Jump & double jump logic can be a bit tricky to get your head round if you want to get it right, especially if you also want float (press jump while going down to fall slower), and then add animations to it. But it's a fun challenge

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

Poll by Flashkit.com