HaxeFlixel Logo HaxeFlixel Snippets
  • About
  • Tutorials
  • Back to HaxeFlixel.com
  • Basics
    • Screen
    • Adding
  • Objects
    • Alive
    • Health
  • Sprites
    • Making Sprites
    • Loading Sprites
    • Animation
    • Alpha
    • Color
    • Facing
    • Scale
    • Baked Rotations
  • Text
    • FlxText
    • FlxBitmapText
  • Movement
    • Velocity
    • Acceleration
    • MaxVelocity
    • Gravity
    • Jumping
    • Angle
    • Angular Velocity
    • Angular Acceleration
  • Groups
    • Basic Group
    • Typed Group
    • Recycling
  • Tiling
    • Tileblock
    • Tilemap
  • Overlap
    • Simple Overlap
    • Overlap Callbacks
  • Collision
    • 1-to-1 Collision
    • Immovable
    • Tilemap Collision
    • Moving Platforms
  • Camera
    • Flash
    • Fade
    • Shake
    • Follow
  • Input
    • Basics
    • Keyboard
    • Mouse
    • Button
  • Sound
    • FlxSound

Jumping

There are many ways to implement a jumping mechanic, but the simplest method is to add negative y velocity temporarily to a FlxObject with positive y acceleration, using some kind of timer to limit the jump time.

FlxG.collide(box, sprite);

var jumpPressed:Bool = pad.buttonA.pressed;

if (jumping && !jumpPressed)
    jumping = false;

// reset jumpTimer when touching the floor
if (sprite.isTouching(FlxObject.DOWN) && !jumping)
    jumpTimer = 0;

if (jumpTimer >= 0 && jumpPressed)
{
    jumping = true;
    jumpTimer += elapsed;
}
else
    jumpTimer = -1;

// hold button to jump higher (up to 0.25s)
if (jumpTimer > 0 && jumpTimer < 0.25)
    sprite.velocity.y = -300;

Demonstration

Source

source/PlayState.hx

package;

import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.ui.FlxVirtualPad;

class PlayState extends FlxState
{
    var sprite:FlxSprite;
    var box:FlxSprite;
    var pad:FlxVirtualPad;

    var jumpTimer:Float = 0;
    var jumping:Bool = false;

    override public function create()
    {
        super.create();

        sprite = new FlxSprite("assets/sprite.png");
        sprite.x = FlxG.width / 2 - sprite.width / 2;
        sprite.acceleration.y = 900;
        sprite.maxVelocity.y = 300;
        add(sprite);

        box = new FlxSprite("assets/bigbox.png");
        box.x = FlxG.width / 2 - box.width / 2;
        box.y = FlxG.height * 0.75 - box.height / 2;
        box.immovable = true;
        add(box);

        pad = new FlxVirtualPad(FlxDPadMode.NONE, FlxActionMode.A);
        add(pad);
    }

    override public function update(elapsed:Float)
    {
        super.update(elapsed);

        FlxG.collide(box, sprite);

        var jumpPressed:Bool = pad.buttonA.pressed;

        if (jumping && !jumpPressed)
            jumping = false;

        // reset jumpTimer when touching the floor
        if (sprite.isTouching(FlxObject.DOWN) && !jumping)
            jumpTimer = 0;

        if (jumpTimer >= 0 && jumpPressed)
        {
            jumping = true;
            jumpTimer += elapsed;
        }
        else
            jumpTimer = -1;

        // hold button to jump higher (up to 0.25s)
        if (jumpTimer > 0 && jumpTimer < 0.25)
            sprite.velocity.y = -300;
    }
}
View Source on GitHub

See Also

  • Collision
  • Movement/Velocity
  • Movement/Acceleration

Tags

movement jumping game mechanic
  1. Home
  2. Movement
  3. Jumping
Powered By HaxeFlixel Logo HaxeFlixel