Difference between revisions of "EventDispatcher:addEventListener"
From GiderosMobile
| (9 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | |||
'''Available since:''' Gideros 2011.6<br/> | '''Available since:''' Gideros 2011.6<br/> | ||
| + | '''Class:''' [[EventDispatcher]]<br/> | ||
| + | |||
=== Description === | === Description === | ||
| − | + | Registers a listener function and an optional data value so that the listener function is called when an event of a particular type occurs. | |
| − | Registers a listener function and an optional data value so that the listener function is called when an event | + | <syntaxhighlight lang="lua"> |
| − | of a particular type occurs. | + | EventDispatcher:addEventListener(type,listener,data) |
| − | + | </syntaxhighlight> | |
| − | < | + | |
| − | |||
| − | </ | ||
=== Parameters === | === Parameters === | ||
| − | '''type''': (string) | + | '''type''': (string) the type of event<br/> |
| − | '''listener''': (function) | + | '''listener''': (function) the listener function that processes the event<br/> |
| − | '''data''': (any) | + | '''data''': (any) an optional data parameter that is passed as a first argument to the listener function '''optional'''<br/> |
| + | |||
| + | === Example === | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | local function callback(data, e) | ||
| + | data = data or {} | ||
| + | data.source = data.source or Pixel.new(0x00ff00, 1, 64, 64) | ||
| + | data.source.sx = data.source.sx or 1 | ||
| + | data.source.sy = data.source.sy or 1 | ||
| + | local x = e.touch.x | ||
| + | local y = e.touch.y | ||
| + | end | ||
| + | |||
| + | local pix = Pixel.new(0xff00ff, 1, 64, 64) | ||
| + | pix.sx = pix:getScaleX() | ||
| + | pix.sy = pix:getScaleY() | ||
| + | |||
| + | stage:addChild(pix) | ||
| + | stage:addEventListener(Event.TOUCHES_END, callback, { source=pix, }) -- type,listener,data | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | {{EventDispatcher}} | ||
Latest revision as of 02:06, 7 December 2025
Available since: Gideros 2011.6
Class: EventDispatcher
Description
Registers a listener function and an optional data value so that the listener function is called when an event of a particular type occurs.
EventDispatcher:addEventListener(type,listener,data)
Parameters
type: (string) the type of event
listener: (function) the listener function that processes the event
data: (any) an optional data parameter that is passed as a first argument to the listener function optional
Example
local function callback(data, e)
data = data or {}
data.source = data.source or Pixel.new(0x00ff00, 1, 64, 64)
data.source.sx = data.source.sx or 1
data.source.sy = data.source.sy or 1
local x = e.touch.x
local y = e.touch.y
end
local pix = Pixel.new(0xff00ff, 1, 64, 64)
pix.sx = pix:getScaleX()
pix.sy = pix:getScaleY()
stage:addChild(pix)
stage:addEventListener(Event.TOUCHES_END, callback, { source=pix, }) -- type,listener,data