Difference between revisions of "EventDispatcher"
(----) |
|||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | + | ||
− | + | '''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/> | |
− | ''' | + | '''Available since:''' Gideros 2011.6<br/> |
− | ''' | + | '''Inherits from:''' [[Object]]<br/> |
− | ''' | + | |
− | === | + | === Description === |
− | + | All classes that dispatch events inherit from [[EventDispatcher]]. The target of an event is a listener function and an optional data value. | |
− | All classes that dispatch events inherit from [[ | + | |
− | When an event is dispatched, the registered function is called. | + | When an event is dispatched, the registered function is called. |
− | If the optional data value is given, it is used as a first parameter while calling the listener function. | + | |
− | Event dispatching and event targets are the core part of the Gideros event model. Different event types (such as [[ | + | If the optional data value is given, it is used as a first parameter while calling the listener function. |
− | On the other hand, [[ | + | |
− | + | Event dispatching and event targets are the core part of the Gideros event model. Different event types (such as [[Event.ENTER_FRAME]], [[Event.TOUCHES_BEGIN]] or [[Event.MOUSE_DOWN]]) flow through the scene tree hierarchy differently. When a touch or mouse event occurs, Gideros dispatches an event object into the event flow from the root of the scene tree. | |
− | If you want to define a class that dispatches events, you can inherit your class from [[ | + | |
− | === | + | On the other hand, [[Event.ENTER_FRAME]] event is dispatched to all [[Sprite]] objects. |
− | + | ||
− | <source lang="lua">-- example 1 | + | If you want to define a class that dispatches events, you can inherit your class from [[EventDispatcher]]. |
+ | |||
+ | === Examples === | ||
+ | |||
+ | <source lang="lua"> | ||
+ | -- example 1 | ||
ClassA = Core.class(EventDispatcher) | ClassA = Core.class(EventDispatcher) | ||
ClassB = Core.class(EventDispatcher) | ClassB = Core.class(EventDispatcher) | ||
Line 27: | Line 32: | ||
local b = ClassB.new() | local b = ClassB.new() | ||
− | b:addEventListener("myevent", a.funcA, a) -- when b dispatches | + | b:addEventListener("myevent", a.funcA, a) -- when b dispatches a "myevent" event, |
− | -- a.funcA will be called with 'a' | + | -- a.funcA will be called with 'a' as first parameter |
− | |||
− | |||
b:dispatchEvent(Event.new("myevent")) -- will print "funcA" | b:dispatchEvent(Event.new("myevent")) -- will print "funcA" | ||
− | |||
-- example 2 | -- example 2 | ||
Line 42: | Line 44: | ||
ball = Ball.new() | ball = Ball.new() | ||
− | ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball)</source> | + | ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball) |
+ | </source> | ||
+ | <br/> | ||
+ | |||
{|- | {|- | ||
+ | |||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
− | === | + | === Methods === |
− | [[ | + | [[EventDispatcher.new]] ''creates a new EventDispatcher object''<br/> |
− | [[ | + | [[EventDispatcher:addEventListener]] ''registers a listener function''<br/> |
− | [[ | + | [[EventDispatcher:dispatchEvent]] ''dispatches an event''<br/> |
− | [[ | + | [[EventDispatcher:hasEventListener]] ''checks if the EventDispatcher object has a event listener''<br/> |
− | [[ | + | [[EventDispatcher:removeAllListeners]] ''remove all listeners''<br/> |
− | [[ | + | [[EventDispatcher:removeEventListener]] ''removes a listener function''<br/> |
+ | |||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
− | === | + | |
− | [[ | + | === Events === |
− | [[ | + | [[Event.APPLICATION_BACKGROUND]]<br/> |
− | [[ | + | [[Event.APPLICATION_EXIT]]<br/> |
− | [[ | + | [[Event.APPLICATION_FOREGROUND]]<br/> |
− | [[ | + | [[Event.APPLICATION_RESIZE]]<br/> |
− | [[ | + | [[Event.APPLICATION_RESUME]]<br/> |
− | [[ | + | [[Event.APPLICATION_START]]<br/> |
− | [[ | + | [[Event.APPLICATION_SUSPEND]]<br/> |
− | [[ | + | [[Event.MEMORY_WARNING]]<br/> |
− | === | + | [[Event.OPEN_URL]]<br/> |
+ | |||
+ | === Constants === | ||
+ | |||
|} | |} | ||
+ | |||
+ | ---- | ||
+ | {{Special:PrefixIndex/EventDispatcher}} | ||
+ | <br/> |
Revision as of 19:07, 2 December 2019
Supported platforms:
Available since: Gideros 2011.6
Inherits from: Object
Description
All classes that dispatch events inherit from EventDispatcher. The target of an event is a listener function and an optional data value.
When an event is dispatched, the registered function is called.
If the optional data value is given, it is used as a first parameter while calling the listener function.
Event dispatching and event targets are the core part of the Gideros event model. Different event types (such as Event.ENTER_FRAME, Event.TOUCHES_BEGIN or Event.MOUSE_DOWN) flow through the scene tree hierarchy differently. When a touch or mouse event occurs, Gideros dispatches an event object into the event flow from the root of the scene tree.
On the other hand, Event.ENTER_FRAME event is dispatched to all Sprite objects.
If you want to define a class that dispatches events, you can inherit your class from EventDispatcher.
Examples
-- example 1
ClassA = Core.class(EventDispatcher)
ClassB = Core.class(EventDispatcher)
function ClassA:funcA(event)
print("funcA", self, event:getType(), event:getTarget())
end
local a = ClassA.new()
local b = ClassB.new()
b:addEventListener("myevent", a.funcA, a) -- when b dispatches a "myevent" event,
-- a.funcA will be called with 'a' as first parameter
b:dispatchEvent(Event.new("myevent")) -- will print "funcA"
-- example 2
Ball = Core.class(Sprite)
function Ball:onEnterFrame()
self:setX(self:getX() + 1)
end
ball = Ball.new()
ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball)
MethodsEventDispatcher.new creates a new EventDispatcher object |
EventsEvent.APPLICATION_BACKGROUND Constants |