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

FlxSignal

You can use FlxSignal to create a signal that can be triggered by anything.

Functions you add to your FlxSignal will be called when the signal is triggered.

// I want sprite1 and sprite2 to call each of their own functions on a certain condition
var sprite1 = new FlxSprite();
var sprite2 = new FlxSprite();

// FlxSignals can take parameters for data or none at all, and when you call `dispatch` it'll run each of its functions.
var signal = new FlxSignal();

// when the signal dispatches, it'll call the public functions from these sprites
signal.add(sprite1.publicFunction);
signal.add(sprite2.otherPublicFunction);

if (condition) {
  // both sprite1.publicFunction and sprite2.otherPublicFunction will both be called because of this dispatch
  signal.dispatch();
}

Demonstration

Source

View Source on GitHub

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.util.FlxSignal;
import flixel.util.FlxTimer;

class PlayState extends FlxState
{
	private var signal:FlxSignal;

	override public function create()
	{
		bgColor = 0;

		signal = new FlxSignal();

		var sprite:SignalSprite = new SignalSprite();
		sprite.x = (FlxG.width / 4) - (sprite.width / 2);
		sprite.y = FlxG.height - sprite.height;
		signal.add(sprite.bounce);
		add(sprite);

		sprite = new SignalSprite();
		sprite.x = (FlxG.width * .75) - (sprite.width / 2);
		sprite.y = FlxG.height - sprite.height;
		signal.add(sprite.bounce);
		add(sprite);

		var timer:FlxTimer = new FlxTimer();
		timer.start(5, callSignal, 0);

		super.create();
	}

	private function callSignal(_):Void
	{
		signal.dispatch();
	}
}

class SignalSprite extends FlxSprite
{
	public function new()
	{
		super('assets/sprite.png');
		acceleration.y = 200;
	}

	public function bounce():Void
	{
		velocity.y = -200;
	}

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

		y = Math.min(y, FlxG.height - height);
	}
}

Tags

timing flxsignal
Powered By HaxeFlixel Logo HaxeFlixel