Skip to content

Signal Event System

Mark Knol edited this page Jun 24, 2013 · 24 revisions

Flambe does not know the concept of events but uses an own implementation of signals.

Listening to signals

You can listen to a signal using the connect-function.
Listen to stage resize signal

// System.stage.resize is a Signal, listen to it.
System.stage.resize.connect(onResize);

private function onResize()
{
   trace('stage has just resized!');
}

Create own signal

// create signal (which dispatches one string).
public var messageSignal:Signal1<String> = new Signal1<String>(); 

// add listener
messageSignal.connect(onMessage);
private function onMessage(value:String)
{
   trace(value); 
}

// dispatch
signal.emit("hello"); 

One-shot connections

Tells the connection to dispose itself after being used once.

signal.connect(function() {...}).once();

Dispose signals

You can 'connect' signals yourself, but these are not automatically disposed. Therefore you should always add a Disposer instance to the owner of the Component class, and connect the signals via the Disposer. You only have to know how much parameters are given to the callback function, and choose connect0, connect1 or connect2 functions. When the Disposer is disposed, the signal-connections are released (removed) too. If you want to remove signals manually, the connect functions return an SignalConnection, which is disposable.

Using a Disposer instance

_disposer.connect0(System.stage.resize, onResize);

private function onResize():Void
{
	
}

.. make sure your Disposer instance is added to an Entity, otherwise you have to call its dispose() yourself.

Full example; Create auto removing listeners / signal connections

private var _disposer:Disposer;
	
override public function onAdded():Void 
{
	super.onAdded();
	
        _disposer = owner.get(Disposer); // grab disposer out of entity
	if (disposer== null) owner.add(_disposer = new Disposer()); // .. or add one when there is none
	
	// connect using Disposer, instead of 'System.pointer.down.connect(onPointerDown)'
	_disposer.connect1(mySprite.pointerDown, onPointerDown);
	_disposer.connect1(mySprite.pointerUp, onPointerUp);
}

private function onPointerDown(event:PointerEvent):Void { 
	trace('pointer down');
}
private function onPointerUp(event:PointerEvent):Void { 
	trace('pointer  up');
}

override public function dispose():Void
{	
	// No need to remove the Disposer instance, since all components are removed by default on dispose
	// No need to remove signal connections. The Disposer automatically removes connections
	super.dispose();
}
Clone this wiki locally