Difference between revisions of "EventDispatcher"
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | '''Supported platforms:''' android | + | '''<translate>Supported platforms</translate>:''' [[File:Platform android]][[File:Platform ios]][[File:Platform mac]][[File:Platform pc]]<br/> |
− | '''Available since:''' Gideros 2011.6<br/> | + | '''<translate>Available since</translate>:''' Gideros 2011.6<br/> |
+ | '''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Object|Object]]<br/> | ||
=== <translate>Description</translate> === | === <translate>Description</translate> === | ||
<translate><br /> | <translate><br /> | ||
− | All classes that dispatch events inherit from | + | All classes that dispatch events inherit from `EventDispatcher`. The target of an event is a listener function and an optional data value.<br /> |
When an event is dispatched, the registered function is called.<br /> | When an event is dispatched, the registered function is called.<br /> | ||
If the optional data value is given, it is used as a first parameter while calling the listener function.<br /> | If the optional data value is given, it is used as a first parameter while calling the listener function.<br /> | ||
− | Event dispatching and event targets are the core part of the Gideros event model. Different event types (such as | + | 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.<br /> |
− | On the other hand, | + | On the other hand, `Event.ENTER_FRAME` event is dispatched to all `Sprite` objects.<br /> |
<br /> | <br /> | ||
− | If you want to define a class that dispatches events, you can inherit your class from | + | If you want to define a class that dispatches events, you can inherit your class from `EventDispatcher`.<br /></translate> |
=== <translate>Examples</translate> === | === <translate>Examples</translate> === | ||
'''Example'''<br/> | '''Example'''<br/> |
Revision as of 09:27, 24 August 2018
Supported platforms: File:Platform androidFile:Platform iosFile:Platform macFile:Platform pc
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
-- 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 an "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 |