Corona SDK event.target touch/tap

1.3k views Asked by At

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 )
1

There are 1 answers

1
AbaEesa On BEST ANSWER
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 )