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

Velocity

Set velocity on a FlxObject to make it move at a constant rate, in pixels per second.

// object will move from left to right at 50 pixels per second
object.velocity.x = 50;

Demonstration

Source

source/PlayState.hx

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;

class PlayState extends FlxState
{
    var sprite:FlxSprite;

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

        sprite = new FlxSprite("assets/sprite.png");
        sprite.y = FlxG.height / 2 - sprite.height / 2;
        add(sprite);

        sprite.velocity.x = 100;
    }

    override public function update(elapsed:Float)
    {
        if (sprite.velocity.x > 0 && sprite.x >= FlxG.width - sprite.width)
        {
            sprite.x = FlxG.width - sprite.width;
            sprite.velocity.x *= -1;
        }
        else if (sprite.velocity.x < 0 && sprite.x <= 0)
        {
            sprite.x = 0;
            sprite.velocity.x *= -1;
        }

        super.update(elapsed);
    }
}
View Source on GitHub

Tags

movement velocity
  1. Home
  2. Movement
  3. Velocity
Powered By HaxeFlixel Logo HaxeFlixel