Creating a Flash platform game with Flixel and Flex - Jet pack

Dec 16th, 2009 by mcasperson

In this tutorial we give the player a jet pack.

PLAY THE DEMO

DOWNLOAD THE SOURCE CODE

RETURN TO THE TUTORIAL INDEX

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.

mcasperson

Written by mcasperson

Rate this Article:

Be the first to rate me.

Add new comment

* You must be logged in order to leave comments, please Sign in or join us.

Comments

No comments yet, be the first to comment on this article.

Related Content