Difference between revisions of "EventDispatcher"

From GiderosMobile
 
(23 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''Supported platforms:''' android, ios, mac, pc<br/>
+
<!-- GIDEROSOBJ:EventDispatcher -->
 +
'''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/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Inherits from:''' [[Object]]<br/>
 +
 
=== Description ===
 
=== Description ===
<translate><br />
+
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 [[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. If the optional data value is given, it is used as a first parameter while calling the listener function.
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.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 />
+
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.<br />
+
 
<br />
+
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]].<br /></translate>
+
 
 +
If you want to define a class that dispatches events, you can inherit your class from EventDispatcher.
 +
 
 
=== Examples ===
 
=== Examples ===
'''Example'''<br/>
+
'''User created event'''
<source lang="lua">-- example 1
+
<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">
 
ClassA = Core.class(EventDispatcher)
 
ClassA = Core.class(EventDispatcher)
 
ClassB = Core.class(EventDispatcher)
 
ClassB = Core.class(EventDispatcher)
  
 
function ClassA:funcA(event)
 
function ClassA:funcA(event)
print(&quot;funcA&quot;, self, event:getType(), event:getTarget())
+
print("funcA", self, event:getType(), event:getTarget())
 
end
 
end
  
Line 24: Line 49:
 
local b = ClassB.new()
 
local b = ClassB.new()
  
b:addEventListener(&quot;myevent&quot;, a.funcA, a) -- when b dispatches an &quot;myevent&quot; event,
+
b:addEventListener("myevent", a.funcA, a)
-- a.funcA will be called with &#039;a&#039;
+
-- when b dispatches an "myevent" event, a.funcA will be called with 'a' as first parameter
-- as first parameter
 
 
 
b:dispatchEvent(Event.new(&quot;myevent&quot;)) -- will print &quot;funcA&quot;
 
 
 
 
 
-- example 2
 
Ball = Core.class(Sprite)
 
  
function Ball:onEnterFrame()
+
b:dispatchEvent(Event.new("myevent")) -- will print "funcA"
self:setX(self:getX()   1)
+
</syntaxhighlight>
end
 
  
ball = Ball.new()
 
ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball)</source>
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== Methods ===
 
=== Methods ===
[[EventDispatcher.new]] ''<translate>creates a new EventDispatcher object</translate>''<br/>
+
[[EventDispatcher.new]] ''creates a new EventDispatcher object''<br/><!--GIDEROSMTD:EventDispatcher.new() creates a new EventDispatcher object-->
[[EventDispatcher:addEventListener]] ''<translate>registers a listener function</translate>''<br/>
+
 
[[EventDispatcher:dispatchEvent]] ''<translate>dispatches an event</translate>''<br/>
+
[[EventDispatcher:addEventListener]] ''registers a listener function''<br/><!--GIDEROSMTD:EventDispatcher:addEventListener(type,listener,data) registers a listener function-->
[[EventDispatcher:hasEventListener]] ''<translate>checks if the EventDispatcher object has a event listener</translate>''<br/>
+
[[EventDispatcher:dispatchEvent]] ''dispatches an event''<br/><!--GIDEROSMTD:EventDispatcher:dispatchEvent(event) dispatches an event-->
[[EventDispatcher:removeAllListeners]] ''<translate>remove all listeners</translate>''<br/>
+
[[EventDispatcher:hasEventListener]] ''checks if the EventDispatcher object has a event listener''<br/><!--GIDEROSMTD:EventDispatcher:hasEventListener(type) checks if the EventDispatcher object has a event listener-->
[[EventDispatcher:removeEventListener]] ''<translate>removes a listener function</translate>''<br/>
+
[[EventDispatcher:removeAllListeners]] ''remove all listeners''<br/><!--GIDEROSMTD:EventDispatcher:removeAllListeners() remove all listeners-->
 +
[[EventDispatcher:removeEventListener]] ''removes a listener function''<br/><!--GIDEROSMTD:EventDispatcher:removeEventListener(type,listener,data) removes a listener function-->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== Events ===
 
=== Events ===
[[Event.APPLICATION_BACKGROUND]]<br/>
+
[[Event.APPLICATION_BACKGROUND]]<br/><!--GIDEROSEVT:Event.APPLICATION_BACKGROUND applicationBackground-->
[[Event.APPLICATION_EXIT]]<br/>
+
[[Event.APPLICATION_EXIT]]<br/><!--GIDEROSEVT:Event.APPLICATION_EXIT applicationExit-->
[[Event.APPLICATION_FOREGROUND]]<br/>
+
[[Event.APPLICATION_FOREGROUND]]<br/><!--GIDEROSEVT:Event.APPLICATION_FOREGROUND applicationForeground-->
[[Event.APPLICATION_RESIZE]]<br/>
+
[[Event.APPLICATION_RESIZE]]<br/><!--GIDEROSEVT:Event.APPLICATION_RESIZE applicationResize-->
[[Event.APPLICATION_RESUME]]<br/>
+
[[Event.APPLICATION_RESUME]]<br/><!--GIDEROSEVT:Event.APPLICATION_RESUME applicationResume-->
[[Event.APPLICATION_START]]<br/>
+
[[Event.APPLICATION_START]]<br/><!--GIDEROSEVT:Event.APPLICATION_START applicationStart-->
[[Event.APPLICATION_SUSPEND]]<br/>
+
[[Event.APPLICATION_SUSPEND]]<br/><!--GIDEROSEVT:Event.APPLICATION_SUSPEND applicationSuspend-->
[[Event.MEMORY_WARNING]]<br/>
+
[[Event.MEMORY_WARNING]]<br/><!--GIDEROSEVT:Event.MEMORY_WARNING memoryWarning-->
[[Event.OPEN_URL]]<br/>
+
[[Event.OPEN_URL]]<br/><!--GIDEROSEVT:Event.OPEN_URL openUrl-->
 
=== Constants ===
 
=== Constants ===
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 01:26, 18 November 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
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"

Methods

EventDispatcher.new creates a new EventDispatcher object

EventDispatcher:addEventListener registers a listener function
EventDispatcher:dispatchEvent dispatches an event
EventDispatcher:hasEventListener checks if the EventDispatcher object has a event listener
EventDispatcher:removeAllListeners remove all listeners
EventDispatcher:removeEventListener removes a listener function

Events

Event.APPLICATION_BACKGROUND
Event.APPLICATION_EXIT
Event.APPLICATION_FOREGROUND
Event.APPLICATION_RESIZE
Event.APPLICATION_RESUME
Event.APPLICATION_START
Event.APPLICATION_SUSPEND
Event.MEMORY_WARNING
Event.OPEN_URL

Constants