문제

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 )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top