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

Alive

HaxeFlixel objects use an alive flag to easily set/determine how they should act. Dead (alive == false) objects do not have update() called on them, by default. You can use FlxBasic.kill() to set alive and exists to false simultaneously, and FlxBasic.revive() to set them to true.

// kill object
object.kill();

// revive object
object.revive();

Demonstration

Source

source/PlayState.hx

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.util.FlxAxes;
import flixel.util.FlxColor;

class PlayState extends FlxState
{
    var sprite:FlxSprite;
    var healthText:FlxText;

    var time:Float = 1;

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

        sprite = new FlxSprite("assets/sprite.png");
        sprite.screenCenter();
        add(sprite);

        healthText = new FlxText();
        healthText.size = 16;
        healthText.text = "Alive";
        healthText.autoSize = false;
        healthText.wordWrap = false;
        healthText.fieldWidth = FlxG.width;
        healthText.color = FlxColor.WHITE;
        healthText.setBorderStyle(FlxTextBorderStyle.SHADOW, FlxColor.BLACK, 2, 1);
        healthText.alignment = FlxTextAlign.CENTER;
        healthText.screenCenter(FlxAxes.X);
        healthText.y = 8;
        add(healthText);
    }

    override public function update(elapsed:Float)
    {
        time -= elapsed;
        if (time <= 0)
        {
            time = 1;
            if (sprite.alive)
            {
                sprite.kill();
                healthText.text = "Dead";
            }
            else
            {
                sprite.revive();
                healthText.text = "Alive";
            }
        }
        super.update(elapsed);
    }
}
View Source on GitHub

Tags

objects
  1. Home
  2. Objects
  3. Alive
Powered By HaxeFlixel Logo HaxeFlixel