CoronaSDK 2013.1127 | Released: 25 May 2013, 2:00am | What's New | Download Now
You will be redirected to the new docs in a few seconds.
If you are certain you want to see the old docs, click cancel.
Cancel | Continue to new docs
object:addEventListener
Description:
Adds a listener to the object’s list of listeners. When the named event occurs, the listener will be invoked and be supplied with a table representing the event.
Syntax:
object:addEventListener( eventName, listener )
Example:
Listeners can be either functions or table objects.
When a function listener is invoked, it is passed a table representing the event:
local myListener = function( event ) print( "Listener called with event of type " .. event.name ) end Runtime:addEventListener( "touch", myListener ) Runtime:addEventListener( "enterFrame", myListener )
Sometimes a function listener is not convenient because certain variables are not in scope when the listener is triggered (invoked). In these situations, object listeners should be used. Object listeners must have an instance method with a name corresponding to the event name:
-- assume MyClass and MyClass:new() already exist function MyClass:enterFrame( event ) print( "enterFrame called at time: " .. event.time ) end function MyClass:touch( event ) print( "touch occurred at ("..event.x..","..event.y..")" ) end local myObject = MyClass:new() Runtime:addEventListener( "touch", myObject ) Runtime:addEventListener( "enterFrame", myObject )
Parameters:
eventName
String specifying the name of the event to listen for.
listener
If the event's event.name matches this string, listener will be invoked. Event listeners are either functions or objects (a.k.a. table listeners).
Returns:
Nothing.
Remarks:
Note: You cannot add an object event listener within the listener event for that object. You should add the listener event outside of the current listener using the timer.performWithDelay() API. Failing to do so will cause the new listener to be called immediately after the current listener returns.
Another solution to the problem is using a single event listener for the object and adding a "state" variable to control what function is performed when invoked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | local addListener1, addListener2 -- forward references -- create a large button local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) rect:setFillColor( 128, 64, 64 ) -- state1 function state1Cb( event ) print("state1") rect:removeEventListener( "tap", state1Cb ) --rect:addEventListener( "tap", state2Cb ) -- ** Don't do this timer.performWithDelay( 1, addListener2 ) -- ** Do this instead return true end -- state2 function state2Cb( event ) print("state2") rect:removeEventListener( "tap", state2Cb ) --rect:addEventListener( "tap", state1Cb ) -- ** Don't do this timer.performWithDelay( 1, addListener1 ) -- ** Do this instead return true end function addListener2( ) rect:addEventListener( "tap", state2Cb ) end function addListener1() rect:addEventListener( "tap", state1Cb ) end -- start addListener1() -- add first listener |
Supported on operating systems and platforms for build numbers shown:
- Mac OS X:Corona SDK 1.0
- Windows:Corona SDK 2.0
- iOS:Corona SDK 1.0
- Android:Corona SDK 2.0
would be really useful if this event mechanism was on all classes, not just display objects and runtime.
for a solution click here