-
Notifications
You must be signed in to change notification settings - Fork 6
Signal Event System
Flambe does not know the concept of events but uses an own implementation of 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 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");
Tells the connection to dispose itself after being used once.
signal.connect(function() {...}).once();
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.
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();
}
Documentation guide for Flambe - Targeted to version 4.0+
Flambe | Installation | Demo projects | Showcase | API Reference | Forum
Flambe is MIT licensed and available for free. Feel free to contribute!
- Home / Installation
- Entity / Components
- Core
- Assets
- Publish your game
- Other
- Editors
- Plugins, tools, extensions
- Help
- More Flambe