Corona SDK event.target touch / tap

时间:2013-12-18 20:20:41

标签: events lua touch corona tap

我正在尝试使用event.target来查看已点击的对象。我有一个由本地事件监听器调用的函数:

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 )

1 个答案:

答案 0 :(得分:7)

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返回对象的引用,首先需要向所有对象添加属性“name”,然后尝试识别.name,如我在代码中所示。

op1Down:addEventListener( "tap", changeParams )