简体   繁体   中英

Corona SDK event.target touch/tap

I am trying to use event.target to see which object has been tapped. I have this function that is called by a local event listener:

local param1 = 0

function changeParams(event)
  if( event.target == "op1Up" ) then
    param1 = param1 + 1
  elseif( event.target == "op1Down" ) then
    param1 = param1 - 1
  end
  dispMyParams1.text = ( "Param 1: "..param1 )
end

local op1Up = display.newRect( --[[rect parameters]]-- )
op1Up:addEventListener( "tap", changeParams )
local op1Down = display.newRect( --[[rect parameters]]-- )
op1Down:addEventListener( "tap", changeParams )
local param1 = 0

function changeParams(event)
  if( event.target.name == "op1Up" ) then
    param1 = param1 + 1
  elseif( event.target.name == "op1Down" ) then
    param1 = param1 - 1
  end
  dispMyParams1.text = ( "Param 1: "..param1 )
end

local op1Up = display.newRect( --[[rect parameters]]-- )
op1Up.name="op1Up"
op1Up:addEventListener( "tap", changeParams )
local op1Down = display.newRect( --[[rect parameters]]-- )
op1Up.name="op1Down"

event.target return reference to the object, you first need to add a property "name" to all objects then try to identify .name there as I shown in the code.

op1Down:addEventListener( "tap", changeParams )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM