នេះជា
Type
EventListener របស់ Corona SDK
នេះជា
eventlistener
គឺជា any display object (or global Runtime object) ដែលអាចទទួលនៅ events បាន
នេះជា
Methods
របស់វា
object:addEventListener()
វាបន្ថែមនៅ
listener ទៅកាន់ object’s list របស់ listeners។ នៅពេលឈ្មោះរបស់ event កើតឡើងពេលនោះ listener
នឹង invoked & supplied ជាមួយនឹង table
representing the event
Syntax:
object:addEventListener (eventName, listener)
eventName(តំរូវការ) វាជា String ដែលជាឈ្មោះរបស់
event ទៅកាន់ listener
listener(តំរូវការ) វាជា listener អាចជាហ្វាំងសិនឬ
table object។ បើសិនជា event.name matches this
string។ listener នឹង invoked។ ចំណែក event listeners អាចជាហ្វាំងសិនឬ Object
សូមមើលកូដះ
local
object = display.newImage( "image.png" )
function
object:touch( event )
if event.phase == "began" then
print( "You touched the object!"
)
return true
end
end
object:addEventListener(
"touch", object )
ចំណាំះ
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.
local addListener1,
addListener2
--
Create a
large button
local
rect = display.newRect( 0, 0,
display.contentWidth, display.contentHeight )
rect:setFillColor(
1, 0, 0 )
--
State 1
function
state1Cb( event )
print("state1")
rect:removeEventListener( "tap",
state1Cb )
--rect:addEventListener( "tap", state2Cb ) -- Do NOT do this!
timer.performWithDelay( 1,
addListener2 ) -- Do
this instead
return true
end
--
State 2
function
state2Cb( event )
print("state2")
rect:removeEventListener( "tap",
state2Cb )
--rect:addEventListener( "tap", state1Cb ) -- Do NOT 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
No comments:
Post a Comment