HaxeFlixel Logo HaxeFlixel Snippets
  • About
  • Back to HaxeFlixel.com
    • Screen
    • Adding
    • Alive
    • Health
    • Making Sprites
    • Loading Sprites
    • Animation
    • Alpha
    • Color
    • Facing
    • Scale
    • Baked Rotations
    • FlxText
    • FlxBitmapText
    • Velocity
    • Acceleration
    • MaxVelocity
    • Gravity
    • Jumping
    • Angle
    • Angular Velocity
    • Angular Acceleration
    • Basic Group
    • Typed Group
    • Recycling
    • Sorting
    • Tileblock
    • Tilemap
    • Ray
    • tileProperties
    • Autotiles
    • Simple Overlap
    • Overlap Callbacks
    • 1-to-1 Collision
    • Immovable
    • Tilemap Collision
    • Moving Platforms
    • Flash
    • Fade
    • Shake
    • Follow
    • scrollFactor
    • Basics
    • Keyboard
    • Mouse
    • Button
    • FlxSound
    • FlxState
    • FlxSubState
    • Tween
    • Angle
    • Color
    • Motion
    • Num
    • Callbacks
    • FlxTimer
    • FlxSignal
    • moveTowards
    • Distance
    • velocityFromAngle
    • timeScale
    • drawLine

Facing

facing can be used to help set/determine the orientation of a FlxSprite. Combine this with FlxSprite.setFacingFlip() to have your sprite graphic flip depending on the way it’s facing.

// set sprite's facing to RIGHT.
sprite.facing = RIGHT;

// whenever sprite is facing RIGHT, do NOT flip the sprite's graphic
sprite.setFacingFlip(RIGHT, false, false);

// whenever sprite is facing LEFT, flip the graphic horizontally
sprite.setFacingFlip(LEFT, true, false);

Demonstration

Source

View Source on GitHub

package;

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

class PlayState extends FlxState
{
	var sprite:FlxSprite;
	var flipTimer:Float = 1;

	override public function create()
	{
		bgColor = 0;

		super.create();

		sprite = new FlxSprite("assets/arrow.png");
		sprite.screenCenter();
		sprite.facing = RIGHT;
		sprite.setFacingFlip(RIGHT, false, false);
		sprite.setFacingFlip(LEFT, true, false);
		add(sprite);
	}

	override public function update(elapsed:Float)
	{
		flipTimer -= elapsed;
		if (flipTimer <= 0)
		{
			if (sprite.facing == RIGHT)
				sprite.facing = LEFT;
			else
				sprite.facing = RIGHT;

			flipTimer++;
		}
		super.update(elapsed);
	}
}

Tags

sprites graphics
Powered By HaxeFlixel Logo HaxeFlixel