Touch different image

Hi I need a nodge in the right direction.

I have 3 images. And I want some thing to happen if 1,2 or 3 is touched.

local function myTapListener( event )

   – Code executed when the button is tapped

if event.target == T1 then 

print(“one was touched”)

elseif event.target == T2 then 

print(“two”)

end

    return true

end

local T1 = display.newImage(image[r1], 0.5, 0.5)

local T2 = display.newImage(image[r2], 0.5, 0.5)

local T3 = display.newImage(image[r3], 0.5, 0.5)

T1:addEventListener( “tap”, myTapListener )

T2:addEventListener( “tap”, myTapListener )

T3:addEventListener( “tap”, myTapListener )

I don’t see the clear path to touch image 1 -> do what is said and print the text

local function myTapListener( event ) -- Code executed when the button is tapped if event.target.name == "T1" then print("one was touched") elseif event.target.name == "T2" then print("two") end return true end local T1 = display.newImage(image[r1], 0.5, 0.5) T1.name = "T1" local T2 = display.newImage(image[r2], 0.5, 0.5) T2.name = "T2" local T3 = display.newImage(image[r3], 0.5, 0.5) T3.name = "T3" T1:addEventListener( "tap", myTapListener ) T2:addEventListener( "tap", myTapListener ) T3:addEventListener( "tap", myTapListener )

Inside your touch or tap handler, event.target is the object interacted with. You just need a way to ID each item. Most people will just add an extra member to the object. Some people will use .name others will use .id, you could call it .myAwesomeIdentifier if you wanted to. Just assign some string that will uniquely identify the object. In this case I made string values that matched the Object variable’s name.  Now event.target.id will have that value you can compare against.

Rob

Thanks Rob Miracle that solves it! -And I can use that trick some other places.

My first app was pretty much one button and some ui elements that changed. Now that I got experience with displaying objects where I want, I startet my first game project where there are more logic involved. 

local function myTapListener( event ) -- Code executed when the button is tapped if event.target.name == "T1" then print("one was touched") elseif event.target.name == "T2" then print("two") end return true end local T1 = display.newImage(image[r1], 0.5, 0.5) T1.name = "T1" local T2 = display.newImage(image[r2], 0.5, 0.5) T2.name = "T2" local T3 = display.newImage(image[r3], 0.5, 0.5) T3.name = "T3" T1:addEventListener( "tap", myTapListener ) T2:addEventListener( "tap", myTapListener ) T3:addEventListener( "tap", myTapListener )

Inside your touch or tap handler, event.target is the object interacted with. You just need a way to ID each item. Most people will just add an extra member to the object. Some people will use .name others will use .id, you could call it .myAwesomeIdentifier if you wanted to. Just assign some string that will uniquely identify the object. In this case I made string values that matched the Object variable’s name.  Now event.target.id will have that value you can compare against.

Rob

Thanks Rob Miracle that solves it! -And I can use that trick some other places.

My first app was pretty much one button and some ui elements that changed. Now that I got experience with displaying objects where I want, I startet my first game project where there are more logic involved.