- Makes controlling an FlxSprite with the keyboard a LOT easier and quicker to set-up! Sometimes it's hard to know what values to set, especially if you want gravity, jumping, sliding, etc. This class helps sort that - and adds some cool extra functionality too :)
TODO: Hot Keys TODO: Binding of sound effects to keys (seperate from setSounds? as those are event based) TODO: Specify animation frames to play based on velocity TODO: Variable gravity (based on height, the higher the stronger the effect)
@link http://www.photonstorm.com
Static variables
staticinlineread onlyKEYMODE_JUST_DOWN:Int = 1
This keyboard fires when the key has just been pressed down, and not again until it is released and re-pressed
staticinlineread onlyKEYMODE_RELEASED:Int = 2
This keyboard fires only when the key has been pressed and then released again
staticinlineread onlyMOVEMENT_ACCELERATES:Int = 1
The "Accelerates" Movement Type means the sprite will accelerate until it reaches maximum speed.
staticinlineread onlyMOVEMENT_INSTANT:Int = 0
The "Instant" Movement Type means the sprite will move at maximum speed instantly, and will not "accelerate" (or speed-up) before reaching that speed.
staticinlineread onlyROTATION_ACCELERATES:Int = 1
The "Accelerates" Rotaton Type means the sprite will accelerate until it reaches maximum rotation speed.
staticinlineread onlyROTATION_INSTANT:Int = 0
The "Instant" Movement Type means the sprite will rotate at maximum speed instantly, and will not "accelerate" (or speed-up) before reaching that speed.
staticinlineread onlyROTATION_STOPPING_DECELERATES:Int = 1
The "Decelerates" Stopping Type means the sprite will start decelerating when no rotation keys are being pressed. Deceleration continues until rotation speed reaches zero.
staticinlineread onlyROTATION_STOPPING_INSTANT:Int = 0
The "Instant" Stopping Type means the sprite will stop rotating immediately when no rotation keys are being pressed, there will be no deceleration.
staticinlineread onlyROTATION_STOPPING_NEVER:Int = 2
The "Never" Stopping Type means the sprite will never decelerate, any speed built up will be carried on and never reduce.
staticinlineread onlySTOPPING_DECELERATES:Int = 1
The "Decelerates" Stopping Type means the sprite will start decelerating when no direction keys are being pressed. Deceleration continues until the speed reaches zero.
staticinlineread onlySTOPPING_INSTANT:Int = 0
The "Instant" Stopping Type means the sprite will stop immediately when no direction keys are being pressed, there will be no deceleration.
staticinlineread onlySTOPPING_NEVER:Int = 2
The "Never" Stopping Type means the sprite will never decelerate, any speed built up will be carried on and never reduce.
Constructor
new(sprite:FlxSprite, movementType:Int, stoppingType:Int, updateFacing:Bool = false, enableArrowKeys:Bool = true)
Sets the FlxSprite to be controlled by this class, and defines the initial movement and stopping types. After creating an instance of this class you should call setMovementSpeed, and one of the enableXControl functions if you need more than basic cursors.
Parameters:
sprite | The FlxSprite you want this class to control. It can only control one FlxSprite at once. |
---|---|
movementType | Set to either MOVEMENT_INSTANT or MOVEMENT_ACCELERATES |
StoppingType | Set to STOPPING_INSTANT, STOPPING_DECELERATES or STOPPING_NEVER |
updateFacing | If true it sets the FlxSprite.facing value to the direction pressed (default false) |
enableArrowKeys | If true it will enable all arrow keys (default) - see setCursorControl for more fine-grained control |
Variables
Methods
flipGravity():Void
Switches the gravity applied to the sprite. If gravity was +400 Y (pulling them down) this will swap it to -400 Y (pulling them up) To reset call flipGravity again
resetSpeeds(resetX:Bool = true, resetY:Bool = true):Void
setAdvancedMovementSpeed(leftSpeed:Int, rightSpeed:Int, upSpeed:Int, downSpeed:Int, speedMaxX:Int, speedMaxY:Int, decelerationX:Int = 0, decelerationY:Int = 0):Void
Set the speed at which the sprite will move when a direction key is pressed. All values are given in pixels per second. So an xSpeed of 100 would move the sprite 100 pixels in 1 second (1000ms) Due to the nature of the internal Flash timer this amount is not 100% accurate and will vary above/below the desired distance by a few pixels.
If you don't need different speed values for every direction on its own then use setMovementSpeed
Parameters:
leftSpeed | The speed in pixels per second in which the sprite will move/accelerate to the left |
---|---|
rightSpeed | The speed in pixels per second in which the sprite will move/accelerate to the right |
upSpeed | The speed in pixels per second in which the sprite will move/accelerate up |
downSpeed | The speed in pixels per second in which the sprite will move/accelerate down |
speedMaxX | The maximum speed in pixels per second in which the sprite can move horizontally |
speedMaxY | The maximum speed in pixels per second in which the sprite can move vertically |
decelerationX | Deceleration speed in pixels per second to apply to the sprites horizontal movement (default 0) |
decelerationY | Deceleration speed in pixels per second to apply to the sprites vertical movement (default 0) |
setBounds(x:Int, y:Int, width:Int, height:Int):Void
Limits the sprite to only be allowed within this rectangle. If its x/y coordinates go outside it will be repositioned back inside. Coordinates should be given in GAME WORLD pixel values (not screen value, although often they are the two same things)
Parameters:
x | The x coordinate of the top left corner of the area (in game world pixels) |
---|---|
y | The y coordinate of the top left corner of the area (in game world pixels) |
width | The width of the area (in pixels) |
height | The height of the area (in pixels) |
setCursorControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables Cursor/Arrow Key controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the UP key |
---|---|
allowDown | Enable the DOWN key |
allowLeft | Enable the LEFT key |
allowRight | Enable the RIGHT key |
setCustomKeys(customUpKey:String, customDownKey:String, customLeftKey:String, customRightKey:String):Void
Sets Custom Key controls. Useful if none of the pre-defined sets work. All String values should be taken from flixel.system.input.Keyboard Pass a blank (empty) String to disable that key from being checked.
Parameters:
customUpKey | The String to use for the Up key. |
---|---|
customDownKey | The String to use for the Down key. |
customLeftKey | The String to use for the Left key. |
customRightKey | The String to use for the Right key. |
setDeceleration(speedX:Int, speedY:Int):Void
Deceleration is a speed (in pixels per second) that is applied to the sprite if stopping type is "DECELERATES" and if no acceleration is taking place. The velocity of the sprite will be reduced until it reaches zero, and can be configured separately per axis.
Parameters:
speedX | The speed in pixels per second at which the sprite will have its horizontal speed decreased |
---|---|
speedY | The speed in pixels per second at which the sprite will have its vertical speed decreased |
setDvorakSimplifiedControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables Dvoark Simplified Controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (COMMA) key |
---|---|
allowDown | Enable the down (A) key |
allowLeft | Enable the left (O) key |
allowRight | Enable the right (E) key |
setESDFControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables ESDF (home row) controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (E) key |
---|---|
allowDown | Enable the down (D) key |
allowLeft | Enable the left (S) key |
allowRight | Enable the right (F) key |
setFireButton(key:String, keymode:Int, repeatDelay:Int, callback:() ‑> Void, altKey:String = ""):Void
Enable a fire button
Parameters:
key | The key to use as the fire button (String from flixel.system.input.Keyboard, i.e. "SPACE", "CONTROL") |
---|---|
keymode | The FlxControlHandler KEYMODE value (KEYMODE_PRESSED, KEYMODE_JUST_DOWN, KEYMODE_RELEASED) |
repeatDelay | Time delay in ms between which the fire action can repeat (0 means instant, 250 would allow it to fire approx. 4 times per second) |
callback | A user defined function to call when it fires |
altKey | Specify an alternative fire key that works AS WELL AS the primary fire key (TODO) |
setGravity(forceX:Int, forceY:Int):Void
Gravity can be applied to the sprite, pulling it in any direction. Gravity is given in pixels per second and is applied as acceleration. The speed the sprite reaches under gravity will never exceed the Maximum Movement Speeds set. If you don't want gravity for a specific direction pass a value of zero.
Parameters:
forceX | A positive value applies gravity dragging the sprite to the right. A negative value drags the sprite to the left. Zero disables horizontal gravity. |
---|---|
forceY | A positive value applies gravity dragging the sprite down. A negative value drags the sprite up. Zero disables vertical gravity. |
setHJKLControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables HJKL (Rogue / Net-Hack) controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (K) key |
---|---|
allowDown | Enable the down (J) key |
allowLeft | Enable the left (H) key |
allowRight | Enable the right (L) key |
setIJKLControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables IJKL (right-sided or secondary player) controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (I) key |
---|---|
allowDown | Enable the down (K) key |
allowLeft | Enable the left (J) key |
allowRight | Enable the right (L) key |
setJumpButton(key:String, keymode:Int, height:Int, surface:Int, repeatDelay:Int = 250, jumpFromFall:Int = 0, ?callback:() ‑> Void, altKey:String = ""):Void
Enable a jump button
Parameters:
key | The key to use as the jump button (String from flixel.system.input.Keyboard, i.e. "SPACE", "CONTROL") |
---|---|
keymode | The FlxControlHandler KEYMODE value (KEYMODE_PRESSED, KEYMODE_JUST_DOWN, KEYMODE_RELEASED) |
height | The height in pixels/sec that the Sprite will attempt to jump (gravity and acceleration can influence this actual height obtained) |
surface | A bitwise combination of all valid surfaces the Sprite can jump off (such as FLOOR) |
repeatDelay | Time delay in ms between which the jumping can repeat (250 would be 4 times per second) |
jumpFromFall | A time in ms that allows the Sprite to still jump even if it's just fallen off a platform, if still within ths time limit |
callback | A user defined function to call when the Sprite jumps |
altKey | Specify an alternative jump key that works AS WELL AS the primary jump key (TODO) |
setMaximumRotationSpeed(speed:Float, limitVelocity:Bool = true):Void
Sets the maximum speed (in pixels per second) that the FlxSprite can rotate. When the FlxSprite is accelerating (movement type MOVEMENT_ACCELERATES) its speed won't increase above this value. However Flixel allows the velocity of an FlxSprite to be set to anything. So if you'd like to check the value and restrain it, then enable "limitVelocity".
Parameters:
speed | The maximum speed in pixels per second in which the sprite can rotate |
---|---|
limitVelocity | If true the angular velocity of the FlxSprite will be checked and kept within the limit. If false it can be set to anything. |
setMaximumSpeed(speedX:Int, speedY:Int, limitVelocity:Bool = true):Void
Sets the maximum speed (in pixels per second) that the FlxSprite can move. You can set the horizontal and vertical speeds independantly. When the FlxSprite is accelerating (movement type MOVEMENT_ACCELERATES) its speed won't increase above this value. However Flixel allows the velocity of an FlxSprite to be set to anything. So if you'd like to check the value and restrain it, then enable "limitVelocity".
Parameters:
speedX | The maximum speed in pixels per second in which the sprite can move horizontally |
---|---|
speedY | The maximum speed in pixels per second in which the sprite can move vertically |
limitVelocity | If true the velocity of the FlxSprite will be checked and kept within the limit. If false it can be set to anything. |
setMovementSpeed(speedX:Int, speedY:Int, speedMaxX:Int, speedMaxY:Int, decelerationX:Int = 0, decelerationY:Int = 0):Void
Set the speed at which the sprite will move when a direction key is pressed. All values are given in pixels per second. So an xSpeed of 100 would move the sprite 100 pixels in 1 second (1000ms) Due to the nature of the internal Flash timer this amount is not 100% accurate and will vary above/below the desired distance by a few pixels.
If you need different speed values for left/right or up/down then use setAdvancedMovementSpeed
Parameters:
speedX | The speed in pixels per second in which the sprite will move/accelerate horizontally |
---|---|
speedY | The speed in pixels per second in which the sprite will move/accelerate vertically |
speedMaxX | The maximum speed in pixels per second in which the sprite can move horizontally |
speedMaxY | The maximum speed in pixels per second in which the sprite can move vertically |
decelerationX | A deceleration speed in pixels per second to apply to the sprites horizontal movement (default 0) |
decelerationY | A deceleration speed in pixels per second to apply to the sprites vertical movement (default 0) |
setNumpadControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables Numpad (left-handed) Controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (NUMPADEIGHT) key |
---|---|
allowDown | Enable the down (NUMPADTWO) key |
allowLeft | Enable the left (NUMPADFOUR) key |
allowRight | Enable the right (NUMPADSIX) key |
setRotationDeceleration(speed:Float):Void
Deceleration is a speed (in pixels per second) that is applied to the sprite if stopping type is "DECELERATES" and if no rotation is taking place. The velocity of the sprite will be reduced until it reaches zero.
Parameters:
speed | The speed in pixels per second at which the sprite will have its angular rotation speed decreased |
---|
setRotationKeys(leftRight:Bool = true, upDown:Bool = false, customAntiClockwise:String = "", customClockwise:String = ""):Void
Set which keys will rotate the sprite. The speed of rotation is set in setRotationSpeed.
Parameters:
leftRight | Use the LEFT and RIGHT arrow keys for anti-clockwise and clockwise rotation respectively. |
---|---|
upDown | Use the UP and DOWN arrow keys for anti-clockwise and clockwise rotation respectively. |
customAntiClockwise | The String value of your own key to use for anti-clockwise rotation (as taken from flixel.system.input.Keyboard) |
customClockwise | The String value of your own key to use for clockwise rotation (as taken from flixel.system.input.Keyboard) |
setRotationLimits(minimumAngle:Int, maximumAngle:Int):Void
Set minimum and maximum angle limits that the Sprite won't be able to rotate beyond. Values must be between -180 and +180. 0 is pointing right, 90 down, 180 left, -90 up.
Parameters:
minimumAngle | Minimum angle below which the sprite cannot rotate (must be -180 or above) |
---|---|
maximumAngle | Maximum angle above which the sprite cannot rotate (must be 180 or below) |
setRotationSpeed(antiClockwiseSpeed:Float, clockwiseSpeed:Float, speedMax:Float, deceleration:Float):Void
Set the speed at which the sprite will rotate when a direction key is pressed. Use this in combination with setMovementSpeed to create a Thrust like movement system. All values are given in pixels per second. So an xSpeed of 100 would rotate the sprite 100 pixels in 1 second (1000ms) Due to the nature of the internal Flash timer this amount is not 100% accurate and will vary above/below the desired distance by a few pixels.
setRotationType(rotationType:Int, stoppingType:Int):Void
Sets the rotation type and the rotation stopping type.
Parameters:
rotationType | The rotation type. Must be either |
---|---|
stoppingType | The rotation stopping type. Must be |
setSounds(?jump:FlxSound, ?fire:FlxSound, ?walk:FlxSound, ?thrust:FlxSound):Void
Set sound effects for the movement events jumping, firing, walking and thrust.
Parameters:
jump | The FlxSound to play when the user jumps |
---|---|
fire | The FlxSound to play when the user fires |
walk | The FlxSound to play when the user walks |
thrust | The FlxSound to play when the user thrusts |
setStandardSpeed(speed:Int, acceleration:Bool = true):Void
If you know you need the same value for the acceleration, maximum speeds and (optionally) deceleration then this is a quick way to set them.
Parameters:
speed | The speed in pixels per second in which the sprite will move/accelerate/decelerate |
---|---|
acceleration | If true it will set the speed value as the deceleration value (default) false will leave deceleration disabled |
setThrust(thrustKey:String, thrustSpeed:Float, ?reverseKey:String, reverseSpeed:Float = 0):Void
If you want to enable a Thrust like motion for your sprite use this to set the speed and keys. This is usually used in conjunction with Rotation and it will over-ride anything already defined in setMovementSpeed.
Parameters:
thrustKey | Specify the key String (as taken from flixel.system.input.Keyboard) to use for the Thrust action |
---|---|
thrustSpeed | The speed in pixels per second which the sprite will move. Acceleration or Instant movement is determined by the Movement Type. |
reverseKey | If you want to be able to reverse, set the key string as taken from flixel.system.input.Keyboard (defaults to null). |
reverseSpeed | The speed in pixels per second which the sprite will reverse. Acceleration or Instant movement is determined by the Movement Type. |
setWASDControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables WASD controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (W) key |
---|---|
allowDown | Enable the down (S) key |
allowLeft | Enable the left (A) key |
allowRight | Enable the right (D) key |
setZQSDControl(allowUp:Bool = true, allowDown:Bool = true, allowLeft:Bool = true, allowRight:Bool = true):Void
Enables ZQSD (Azerty keyboard) controls. Can be set on a per-key basis. Useful if you only want to allow a few keys. For example in a Space Invaders game you'd only enable LEFT and RIGHT.
Parameters:
allowUp | Enable the up (Z) key |
---|---|
allowDown | Enable the down (Q) key |
allowLeft | Enable the left (S) key |
allowRight | Enable the right (D) key |