Difference between revisions of "EventDispatcher"

From GiderosMobile
(----)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
 
<!-- 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/>
'''<translate>Supported platforms</translate>:''' [[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/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Inherits from:''' [[Object]]<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Object|Object]]<br/>
+
 
=== <translate>Description</translate> ===
+
=== 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 [[Special:MyLanguage/EventDispatcher|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.<br />
+
 
Event dispatching and event targets are the core part of the Gideros event model. Different event types (such as [[Special:MyLanguage/Event.ENTER_FRAME|Event.ENTER_FRAME]], [[Special:MyLanguage/Event.TOUCHES_BEGIN|Event.TOUCHES_BEGIN]] or [[Special:MyLanguage/Event.MOUSE_DOWN|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 />
+
If the optional data value is given, it is used as a first parameter while calling the listener function.
On the other hand, [[Special:MyLanguage/Event.ENTER_FRAME|Event.ENTER_FRAME]] event is dispatched to all [[Special:MyLanguage/Sprite|Sprite]] objects.<br />
+
 
<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.
If you want to define a class that dispatches events, you can inherit your class from [[Special:MyLanguage/EventDispatcher|EventDispatcher]].<br /></translate>
+
 
=== <translate>Examples</translate> ===
+
On the other hand, [[Event.ENTER_FRAME]] event is dispatched to all [[Sprite]] objects.
'''Example'''<br/>
+
 
<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 an "myevent" event,
+
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
-- 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;"|
  
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/EventDispatcher.new|EventDispatcher.new]] ''<translate>creates a new EventDispatcher object</translate>''<br/><!-- GIDEROSMTD:EventDispatcher.new() creates a new EventDispatcher object -->
+
[[EventDispatcher.new]] ''creates a new EventDispatcher object''<br/>
[[Special:MyLanguage/EventDispatcher:addEventListener|EventDispatcher:addEventListener]] ''<translate>registers a listener function</translate>''<br/><!-- GIDEROSMTD:EventDispatcher:addEventListener(type,listener,data) registers a listener function -->
+
[[EventDispatcher:addEventListener]] ''registers a listener function''<br/>
[[Special:MyLanguage/EventDispatcher:dispatchEvent|EventDispatcher:dispatchEvent]] ''<translate>dispatches an event</translate>''<br/><!-- GIDEROSMTD:EventDispatcher:dispatchEvent(event) dispatches an event -->
+
[[EventDispatcher:dispatchEvent]] ''dispatches an event''<br/>
[[Special:MyLanguage/EventDispatcher:hasEventListener|EventDispatcher:hasEventListener]] ''<translate>checks if the EventDispatcher object has a event listener</translate>''<br/><!-- GIDEROSMTD:EventDispatcher:hasEventListener(type) checks if the EventDispatcher object has a event listener -->
+
[[EventDispatcher:hasEventListener]] ''checks if the EventDispatcher object has a event listener''<br/>
[[Special:MyLanguage/EventDispatcher:removeAllListeners|EventDispatcher:removeAllListeners]] ''<translate>remove all listeners</translate>''<br/><!-- GIDEROSMTD:EventDispatcher:removeAllListeners() remove all listeners -->
+
[[EventDispatcher:removeAllListeners]] ''remove all listeners''<br/>
[[Special:MyLanguage/EventDispatcher:removeEventListener|EventDispatcher:removeEventListener]] ''<translate>removes a listener function</translate>''<br/><!-- GIDEROSMTD:EventDispatcher:removeEventListener(type,listener,data) removes a listener function -->
+
[[EventDispatcher:removeEventListener]] ''removes a listener function''<br/>
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
 
[[Special:MyLanguage/Event.APPLICATION_BACKGROUND|Event.APPLICATION_BACKGROUND]]<br/><!-- GIDEROSEVT:Event.APPLICATION_BACKGROUND applicationBackground-->
+
=== Events ===
[[Special:MyLanguage/Event.APPLICATION_EXIT|Event.APPLICATION_EXIT]]<br/><!-- GIDEROSEVT:Event.APPLICATION_EXIT applicationExit-->
+
[[Event.APPLICATION_BACKGROUND]]<br/>
[[Special:MyLanguage/Event.APPLICATION_FOREGROUND|Event.APPLICATION_FOREGROUND]]<br/><!-- GIDEROSEVT:Event.APPLICATION_FOREGROUND applicationForeground-->
+
[[Event.APPLICATION_EXIT]]<br/>
[[Special:MyLanguage/Event.APPLICATION_RESIZE|Event.APPLICATION_RESIZE]]<br/><!-- GIDEROSEVT:Event.APPLICATION_RESIZE applicationResize-->
+
[[Event.APPLICATION_FOREGROUND]]<br/>
[[Special:MyLanguage/Event.APPLICATION_RESUME|Event.APPLICATION_RESUME]]<br/><!-- GIDEROSEVT:Event.APPLICATION_RESUME applicationResume-->
+
[[Event.APPLICATION_RESIZE]]<br/>
[[Special:MyLanguage/Event.APPLICATION_START|Event.APPLICATION_START]]<br/><!-- GIDEROSEVT:Event.APPLICATION_START applicationStart-->
+
[[Event.APPLICATION_RESUME]]<br/>
[[Special:MyLanguage/Event.APPLICATION_SUSPEND|Event.APPLICATION_SUSPEND]]<br/><!-- GIDEROSEVT:Event.APPLICATION_SUSPEND applicationSuspend-->
+
[[Event.APPLICATION_START]]<br/>
[[Special:MyLanguage/Event.MEMORY_WARNING|Event.MEMORY_WARNING]]<br/><!-- GIDEROSEVT:Event.MEMORY_WARNING memoryWarning-->
+
[[Event.APPLICATION_SUSPEND]]<br/>
[[Special:MyLanguage/Event.OPEN_URL|Event.OPEN_URL]]<br/><!-- GIDEROSEVT:Event.OPEN_URL openUrl-->
+
[[Event.MEMORY_WARNING]]<br/>
=== <translate>Constants</translate> ===
+
[[Event.OPEN_URL]]<br/>
 +
 
 +
=== Constants ===
 +
 
 
|}
 
|}
 +
 +
----
 +
{{Special:PrefixIndex/EventDispatcher}}
 +
<br/>

Revision as of 19:07, 2 December 2019


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

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


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