In this tutorial we give the player a jet pack.
All movement in Flixel can be described in terms of velocity and acceleration. Moving left and right is a matter of setting the acceleration in the x axis to some none zero value. Falling is done by setting the acceleration in y axis to a positive number. Jumping is a little different: it is implemented by subtracting a value (because to move up on the screen means a decrease in the y coordinates) from the velocity in the y axis, giving the player an instant push upward (this is referred to as an Impulse in Newtonian physics).
Adding a jet pack is really just like falling upwards, and so to implement it using Flixels physics we need to set a negative value for the acceleration in the y axis. While the player is pressing the jump key the acceleration in the y axis will be negative. Once the key is released gravity will take over, meaning the acceleration in the y axis will be positive.
First we specify some constants in the Player class that define how the jet pack works.
protected static const JETPACK_ACCELERATION:Number = 50; protected static const JETPACK_COUNTDOWN:Number = 0.5;
The JETPACK_ACCELERATION constant defines the acceleration of the player in the y axis while the jump key in being pressed.
The JETPACK_COUNTDOWN constant defines how long the emitter that shows the jet pack smoke will remain on the screen once the jump key has been released.
[Embed(source="../media/jetpack.mp3")] protected var SndJetpack:Class; [Embed(source="../media/jet.png")] protected var JetImage:Class;
We need to embed some additional graphics and sound effects for the jet pack.
protected var jets:FlxEmitter; protected var jetpackEmitterCountdown:Number = -1;
The jet pack emitter is referenced by a variable called jets, and the jetpackEmitterCountdown variable is defined to keep track of how long the jet pack emitter should remain on the screen.
this.jets = FlxG.state.add(new FlxEmitter(0,0,0,0,null,0.02,0,0,0,0,0,0,0,0,JetImage,15)) as FlxEmitter;
The jet pack emitter is then initialised in the constructor.
The logic for the jet pack is implemented in the Player update function.
if(FlxG.keys.pressed("X"))
{
acceleration.y = -JUMP_ACCELERATION;
if(!this.jets.active)
this.jets.restart();
this.jets.x = this.x + (this.width>>1);
this.jets.y = this.y + this.height;
jetpackEmitterCountdown = -1;
FlxG.play(SndJetpack);
}
Here we replace the code that made the player jump with code that will make it fly. While the X key is pressed the vertical acceleration is set to the JETPACK_ACCELERATION constant. If the jet pack emitter is not active we start it up, and then position the emitter at the players current position. The jetpackEmitterCountdown variable is set to -1 to indicate that the player is flying. Finally a sound effect is played.
else
{
acceleration.y = GRAVITY_ACCELERATION;
}
If the jump key is not being pressed, the players vertical acceleration is set to make it fall with gravity.
if (FlxG.keys.justReleased("X"))
{
jetpackEmitterCountdown = JETPACK_COUNTDOWN;
}
If the player just released the X key we need to start a timer that will remove the jetpack emitter when it reaches 0.
if (jetpackEmitterCountdown != -1)
{
jetpackEmitterCountdown -= FlxG.elapsed;
if (jetpackEmitterCountdown <= 0)
{
jetpackEmitterCountdown = -1;
this.jets.kill();
}
}
If jetpackEmitterCountdown is not equal to -1 (meaning the player is not flying), we count down jetpackEmitterCountdown until it reaches 0, at which point we remove the emitter from the screen.

Get Up to Programming Speed With Online Tutorials...
Here is a look at some great free frameworks to get started developing themes for WordPress....
The concept of software development isn't that new to our generation. But still there are so ma...
In my humble opinion, CMS Drupal has come the most close to the concept of "ideal CMS". A lot o...
Setting up WiFi services for a large event can be difficult...
In this tutorial we add mouse interactivity to the scene....
In this tutorial we allow the view of the isometric scene to be moved with the mouse....
In this tutorial we show the height of an isometric object by adding a shadow....
In this tutorial we modify the appearance of the isometric cube at runtime....
In this tutorial we add some animated isometric boxes to the scene....