luacoronasdkdisplayobject

In CoronaSDK, how can I handle a tap event from a hidden displayObject?


I am displaying a single png image on screen which contains various levels that the player can tap on. I create the image using:

local levelsImage = display.newImage("assets/levelmap-levels.png", 0, 0, true)

I then overlap circles on top of each level as follows (levels is a table containing coordinates and tap radius for each level):

for i,level in ipairs(levels) do
    local levelTapCircle = display.newCircle(level.x, level.y, level.tapRadius)
    levelTapCircle.level=level -- attach the level info to the object so we know which level was tapped
    levelTapCircle.alpha=0.3
    myGroup:insert(levelTapCircle)
end

Attach the tap event handler as follows

levelTapCircle:addEventListener("tap", levelTapped)

In the levelTapped function I simply print the tap coordinates to the output console to prove that the event handler was invoked.

If the circles are visible (i.e. when alpha>0), then the event is raised and the levelTapped function is called. However, if I change the alpha value to 0 in order to hide the circles, the tap event is not called.

levelTapCircle.alpha=0

The following can also be used to hide the circles, but it also results in the tap event handler not being invoked:

levelTapCircle.isVisible = false

My question is:

Is it possible to receive tap events from hidden displayObjects (circles in this case)? If not, how can I receive tap events from specific circular areas on the image, but without actually displaying the circles?


Solution

  • As was said, if alpha = 0 or isVisible = false, touch events will be disabled.

    You can, however, stop this from happening with isHitTestable = true. This keeps you from having to use any sort of workaround.

    object.alpha = 0
    -- OR --
    object.isVisible = false
    
    -- Enable Touch/Tap Events With --
    object.isHitTestable = true