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

Shake

You can cause a FlxCamera to shake by using FlxCamera.shake(). You can specify the intensity, duration, and direction, if you wish.

// shake the camera
FlxG.camera.shake();

Demonstration

Source

source/PlayState.hx

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.tile.FlxTilemap;
import flixel.util.FlxAxes;

class PlayState extends FlxState
{
    var timer:Float = 0;
    var styleNo:Int = -1;
    var style:FlxAxes;

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

        var map = new FlxTilemap();
        map.loadMapFromCSV("assets/map.csv", "assets/tiles.png", 16, 16);
        map.screenCenter();
        add(map);
    }

    override public function update(elapsed:Float)
    {
        timer -= elapsed;
        if (timer <= 0)
        {
            timer = 2;
            styleNo++;
            if (styleNo >= 3)
                styleNo = 0;
            style = switch (styleNo)
            {
                case 0: FlxAxes.XY;
                case 1: FlxAxes.X;
                case _: FlxAxes.Y;
            }

            FlxG.camera.shake(0.05, 0.5, null, true, style);
        }

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

Tags

camera effects
  1. Home
  2. Camera
  3. Shake
Powered By HaxeFlixel Logo HaxeFlixel