// Example of dispatching an event from a component.
class TestComponent extends EntityComponent
{
// Declare identifier for the event.
static public const SOME_ACTION:String = "SomeAction";
public function SomeActionHappens():void
{
owner.eventDispatcher.dispatchEvent(new Event(SOME_ACTION));
}
}
// Example of listening for an event in a component.
class TestListenerComponent extends EntityComponent
{
protected function onAdd():void
{
owner.eventDispatcher.addEventListener(TestComponent.SOME_ACTION, _EventHandler);
}
private function _EventHandler(e:Event):void
{
trace("Got an event!");
}
protected function onRemove():void
{
// Notice that we have to unsubscribe the event when the component
// is removed.
owner.eventDispatcher.removeEventListener(TestComponent.SOME_ACTION, _EventHandler);
}
}
Entity表示实体
EntityComponent表示实体组件
实体组件的几个标准事件处理
class SimplerComponent extends EntityComponent
{
protected override function onAdd():void
{
// Let superclass initialize, too.
super.onAdd();
// Initialize resources.
// This will be called when the component is added to an entity.
}
protected override function onReset():void
{
// Let superclass reset, too.
super.onReset();
// (Re)acquire references to components inside the same entity.
// This will be called after onAdd, whenever another component
// is added or removed from the owning entity.
}
protected override function onRemove():void
{
// Let superclass remove, too.
super.onRemove();
// Release resources and references.
// Called when the component is removed from its entity, usually
// as part of the entity's destruction.
}
}