HaxeFlixel Tutorial–Keyboard Input

 

Now that we have the basics of drawing graphics on the screen down in the ongoing Haxe and HaxeFlixel tutorial series now is a good time to move on to giving our player some control over the game.  Today we are going to look at how you handle keyboard input in a HaxeFlixel game.  It’s a fairly straight forward task, so this will be a fairly short tutorial consisting of a single code sample.  Without further ado, let’s jump right in with the code!

 

package;    import flixel.FlxG;  import flixel.FlxSprite;  import flixel.FlxState;  import flixel.input.keyboard.FlxKey;      class PlayState extends FlxState  {     var sprite:FlxSprite;     override public function create():Void     {        super.create();          // Create a sprite and center it to the middle of the screen        sprite = new FlxSprite(AssetPaths.enemy__png);        sprite.x = FlxG.width/2 - sprite.width/2;        sprite.y = FlxG.height/2 - sprite.height/2;        add(sprite);     }       override public function update(elapsed:Float):Void     {        super.update(elapsed);          if(FlxG.keys.enabled){           if(FlxG.keys.pressed.LEFT)              sprite.x--;           if(FlxG.keys.pressed.RIGHT)              sprite.x++;             if(FlxG.keys.justReleased.UP)              sprite.y--;           if(FlxG.keys.justReleased.DOWN)              sprite.y++;             if(FlxG.keys.anyPressed([FlxKey.ESCAPE,FlxKey.SPACE])){              sprite.x = FlxG.width/2 - sprite.width/2;              sprite.y = FlxG.height/2 - sprite.height/2;           }        }     }  }  

This example draws a sprite centered to the screen and enables you to control it using the arrow keys.  The majority of logic happens in the update function, where we poll the status of various keys using the object FlxG.keys.  Notice that we start by checking if keys is enabled, this is because many platforms Haxe runs on may not have keyboard input at all.  Next we check the status of keys two different ways, using pressed() and justReleased().  There is a critical difference between these two approaches.  Pressed will have a list of all the keys that are currently being pressed, while released will only have a list of keys that were just released in this pass through the game loop.  The end result is, pressed will be true over and over as a key is held down, while justReleased will only be true once per individual key/press cycle.  This means to move up and down you have to keep hitting the UP/DOWN arrows over and over, while you will move left and right constantly as the LEFT/RIGHT arrows are held down.  Keep in mind the game loop takes only a few milliseconds to run, so even though it feels like a single key press, pressed will be true for several frames. 

 

The final portion of this example illustrates how you can query multiple key presses at a single time.  In this case we check if the ESC or SPACE BAR are pressed and reset our sprites position back to the center of the screen if they are. In addition to anyPressed() there are also methods to check just pressed, and just released.  There are also additional methods to check other values such as firstPressed(), firstJustReleased() and more.  You can also get an array of the currently pressed keys using getIsDown().  Ultimately FlxG.keys implements FlxKeyManager, so that is the class to check out to see the full scope of functionality available.

 

One interesting omission from HaxeFlixel is event driven (as opposed to polled) keyboard input.  The underlying OpenFL and Lime libraries do however support it.

 

The Video

Programming HaxeFlixel


Scroll to Top