How can I make a hidden object appear after touching it in corona?

Hello,

I want to make a spot the differences game so I’ll put 2 photos that have 5 differences and on every differences there will be a crosshair or a circle but hidden so when the player taps on the difference the crosshair will appear so I used that code

local image = display.newImage( “crosshair.png”, 30, 50 );
image.alpha = 0

local onTouch = function (event)
if event.phase == “began” then
image.alpha = 1
return true
end
end
Runtime:addEventListener( “touch”, onTouch )

but the problem is the crosshair appears if I touched anywhere on the screen I want to make it appear when I touch its place either than this nothing will appear so please help me. and do u think that it’s the right way to hide the crosshairs and show it after touching it or what?
 

I’m not a big fan of Runtime touch events, so while you could figure out how to do what you want that way, why not put the touch listener on the actual crosshairs?

local function chTapped(event) event.target.alpha = 1 return true end local chPos = { {x=30, y=50}, {x = 100, y = 100}, {x = 200, y = 200} } for i = 1, #chPos do local img = display.newImage("crosshair.png") img.x = chPos[i].x img.y = chPos[i].y img.alpha = .2 img.isHitTestable = true img:addEventListener("tap", chTapped) end  

You’ll notice I also created a table for the x/y position of each crosshair and then loop over that table. Which means you can easily change the number and position of the crosshairs without changing anything other than that table. 

Jay

PS - For testing purposes I also set the alpha to .2 so you can see the objects.

I’m not a big fan of Runtime touch events, so while you could figure out how to do what you want that way, why not put the touch listener on the actual crosshairs?

local function chTapped(event) event.target.alpha = 1 return true end local chPos = { {x=30, y=50}, {x = 100, y = 100}, {x = 200, y = 200} } for i = 1, #chPos do local img = display.newImage("crosshair.png") img.x = chPos[i].x img.y = chPos[i].y img.alpha = .2 img.isHitTestable = true img:addEventListener("tap", chTapped) end  

You’ll notice I also created a table for the x/y position of each crosshair and then loop over that table. Which means you can easily change the number and position of the crosshairs without changing anything other than that table. 

Jay

PS - For testing purposes I also set the alpha to .2 so you can see the objects.