Difference between revisions of "EventDispatcher"

From GiderosMobile
Line 2: Line 2:
 
'''Supported platforms:''' android, ios, mac, pc<br/>
 
'''Supported platforms:''' android, ios, mac, pc<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
=== Description ===
+
=== <translate>Description</translate> ===
 
<translate><br />
 
<translate><br />
 
All classes that dispatch events inherit from [[Special:MyLanguage/EventDispatcher|EventDispatcher]]. The target of an event is a listener function and an optional data value.<br />
 
All classes that dispatch events inherit from [[Special:MyLanguage/EventDispatcher|EventDispatcher]]. The target of an event is a listener function and an optional data value.<br />
Line 11: Line 11:
 
<br />
 
<br />
 
If you want to define a class that dispatches events, you can inherit your class from [[Special:MyLanguage/EventDispatcher|EventDispatcher]].<br /></translate>
 
If you want to define a class that dispatches events, you can inherit your class from [[Special:MyLanguage/EventDispatcher|EventDispatcher]].<br /></translate>
=== Examples ===
+
=== <translate>Examples</translate> ===
 
'''Example'''<br/>
 
'''Example'''<br/>
 
<source lang="lua">-- example 1
 
<source lang="lua">-- example 1
Line 42: Line 42:
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== Methods ===
+
=== <translate>Methods</translate> ===
 
[[Special:MyLanguage/EventDispatcher.new|EventDispatcher.new]] ''<translate>creates a new EventDispatcher object</translate>''<br/>
 
[[Special:MyLanguage/EventDispatcher.new|EventDispatcher.new]] ''<translate>creates a new EventDispatcher object</translate>''<br/>
 
[[Special:MyLanguage/EventDispatcher:addEventListener|EventDispatcher:addEventListener]] ''<translate>registers a listener function</translate>''<br/>
 
[[Special:MyLanguage/EventDispatcher:addEventListener|EventDispatcher:addEventListener]] ''<translate>registers a listener function</translate>''<br/>
Line 50: Line 50:
 
[[Special:MyLanguage/EventDispatcher:removeEventListener|EventDispatcher:removeEventListener]] ''<translate>removes a listener function</translate>''<br/>
 
[[Special:MyLanguage/EventDispatcher:removeEventListener|EventDispatcher:removeEventListener]] ''<translate>removes a listener function</translate>''<br/>
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== Events ===
+
=== <translate>Events</translate> ===
 
[[Special:MyLanguage/Event.APPLICATION_BACKGROUND|Event.APPLICATION_BACKGROUND]]<br/>
 
[[Special:MyLanguage/Event.APPLICATION_BACKGROUND|Event.APPLICATION_BACKGROUND]]<br/>
 
[[Special:MyLanguage/Event.APPLICATION_EXIT|Event.APPLICATION_EXIT]]<br/>
 
[[Special:MyLanguage/Event.APPLICATION_EXIT|Event.APPLICATION_EXIT]]<br/>
Line 60: Line 60:
 
[[Special:MyLanguage/Event.MEMORY_WARNING|Event.MEMORY_WARNING]]<br/>
 
[[Special:MyLanguage/Event.MEMORY_WARNING|Event.MEMORY_WARNING]]<br/>
 
[[Special:MyLanguage/Event.OPEN_URL|Event.OPEN_URL]]<br/>
 
[[Special:MyLanguage/Event.OPEN_URL|Event.OPEN_URL]]<br/>
=== Constants ===
+
=== <translate>Constants</translate> ===
 
|}
 
|}

Revision as of 07:27, 24 August 2018

Supported platforms: android, ios, mac, pc
Available since: Gideros 2011.6

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(&quot;funcA&quot;, self, event:getType(), event:getTarget())
end

local a = ClassA.new()
local b = ClassB.new()

b:addEventListener(&quot;myevent&quot;, a.funcA, a)	-- when b dispatches an &quot;myevent&quot; event,
										-- a.funcA will be called with &#039;a&#039;
										-- as first parameter

b:dispatchEvent(Event.new(&quot;myevent&quot;))		-- will print &quot;funcA&quot;


-- 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)

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