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.
Listeners可以是函數或表對象。
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 假設 MyClass and MyClass:new() 已經存在
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).
如果事件的event.name匹配此字符串,監聽器將被調用。事件監聽器是函數或對象(又稱表位聽眾)。
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.
注意:您不能添加一個事件監聽器對象在監聽事件的對象。你應該加入目前監聽器以外的事件監聽器,目前使用timer.performWithDelay()的API。如果不這樣做將導致新的監聽器被稱為當前監聽後立即返回。
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.
另一種解決問題的辦法是使用一個單獨的事件偵聽器對象,並增加了"狀態"變量來控制函數執行時調用。
--------------------------------
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
Related:
- May 18 Wed 2011 13:54
Corona object:addEventListener
全站熱搜
留言列表
發表留言