Difference between revisions of "EventDispatcher"
Line 6: | Line 6: | ||
=== Description === | === Description === | ||
− | 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. |
− | 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 [[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 === | |
+ | '''User created event''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | local event = Event.new("myevent") | ||
+ | event.data1 = "12345" | ||
+ | event.data2 = "abcde" | ||
+ | mydispatcher:dispatchEvent(event) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Game loop''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | Ball = Core.class(Sprite) | ||
+ | |||
+ | function Ball:onEnterFrame() | ||
+ | self:setX(self:getX() + 1) | ||
+ | end | ||
− | = | + | ball = Ball.new() |
+ | ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''EventDispatcher inheritance''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | |||
ClassA = Core.class(EventDispatcher) | ClassA = Core.class(EventDispatcher) | ||
ClassB = Core.class(EventDispatcher) | ClassB = Core.class(EventDispatcher) | ||
Line 35: | Line 53: | ||
b:dispatchEvent(Event.new("myevent")) -- will print "funcA" | b:dispatchEvent(Event.new("myevent")) -- will print "funcA" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 01:26, 18 November 2023
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
User created event
local event = Event.new("myevent")
event.data1 = "12345"
event.data2 = "abcde"
mydispatcher:dispatchEvent(event)
Game loop
Ball = Core.class(Sprite)
function Ball:onEnterFrame()
self:setX(self:getX() + 1)
end
ball = Ball.new()
ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball)
EventDispatcher inheritance
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"
MethodsEventDispatcher.new creates a new EventDispatcher object EventDispatcher:addEventListener registers a listener function |
EventsEvent.APPLICATION_BACKGROUND Constants |