I’m using the corona’s flashlight sample code to create a interaction where the user searches the scene for stars hidden in the dark. When the touch moves over a star, I want an event to be triggered. What is the best way to achieve this?
This is my code so far:
(It isn’t quite working as the transition only triggers once - maybe I’m going out it the wrong way?)
local radiusMax = math.sqrt( centerX\*centerX + centerY\*centerY ) function findStars(event) if event.x == goldStar.x or event.y == goldStar.y then print("found a star") transition.to(goldStar, {rotation = 360, time = 1000}) end end function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.maskX t.y0 = event.y - t.maskY elseif t.isFocus then if "moved" == phase then findStars(event) local maskX = event.x - t.x0 local maskY = event.y - t.y0 t.maskX = maskX t.maskY = maskY -- Stretch the flashlight as it moves further away -- from the screen's center local radius = math.sqrt( maskX\*maskX + maskY\*maskY ) local scaleDelta = radius/radiusMax t.maskScaleX = 1 + scaleDelta t.maskScaleY = 1 + 0.2 \* scaleDelta -- Rotate it appropriately about screen center local rotation = math.deg( math.atan2( maskY, maskX ) ) t.maskRotation = rotation elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end image:addEventListener( "touch", onTouch )