Unable to specify a table object to a event listener in Lua

39 Views Asked by At

I'm following a Solar2d tutorial (https://docs.coronalabs.com/guide/programming/index.html) and I'm trying to combine the first couple of lessons into a balloon popping game. I would like to have balloon-objects in a table and tapping would cause a balloon to disappear. I added the event listener where I'm defining everything else with my balloons. However tapping doesn't do anything. My questions are:

  1. How can I make a tap remove the balloon image?
  2. Where should I add code to remove a tapped balloon from table? Is a for-loop inside gameloop?

Thanks in advance!

`-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

--prep the physics
local physics = require("physics")
physics.start()
physics.setGravity(0, -20)

--seed the random number generator
math.randomseed( os.time() )

--prep the graphic groups
local backGroup = display.newGroup()
local mainGroup = display.newGroup()
local uiGroup = display.newGroup()

--prep variables
local newBalloon
local tappedBalloon
local balloonTable = {}

--load background
local background = display.newImageRect(backGroup,  "pictures/mainGroup/background.png", 360, 570)
background.x = display.contentCenterX
background.y = display.contentCenterY

--load boundaries
local border = display.newImageRect(mainGroup, "pictures/mainGroup/border.png", 281, 35)
border.x = display.contentCenterX
border.y = display.contentHeight-440
physics.addBody( border, "static" )

local function popBalloon( event )
    local tappedBalloon = event.target
    if event.phase == "began" then
        display.remove(tappedBalloon)
        --removing balloon from table?
    end
end


--creating and storing balloons
local function createBalloon ()
    local newBalloon = display.newImageRect(mainGroup, "pictures/mainGroup/balloon2.png", 118, 118)
    if newBalloon then
        table.insert(balloonTable, newBalloon)
        physics.addBody( newBalloon, "dynamic", { radius=50, bounce=0.2 } )
        --newBalloon.isBullet = true
        newBalloon.alpha = 0.8
        newBalloon.myName = "balloon"
        newBalloon:addEventListener("tap", popBalloon)
    end
    
    local placement = math.random(3)

    if (placement == 1) then
        newBalloon.x = math.random( display.contentWidth )
        newBalloon.y = math.random( 250,500 )
    elseif ( placement == 2 ) then
        newBalloon.x = math.random( display.contentWidth )
        newBalloon.y = math.random( 250,500 )
    elseif ( placement == 3 ) then
        newBalloon.x = math.random( display.contentWidth )
        newBalloon.y = math.random( 250,500 )
    end
end

local function gameLoop()
    createBalloon()
end

gameLoopTimer = timer.performWithDelay( 500, gameLoop)`

I tried having the event listener outside the balloon creation but resulted in a "nil value" error code.

1

There are 1 best solutions below

0
krystal On

"Tap" event listeners don't have event phases. If you wanna control phases you should use "touch" listener. It's fine to remove your object like this;

local function popBalloon( event )
   local tappedBalloon = event.target
   display.remove(tappedBalloon)
end

Also your game loop only works once because performWithDelay's third argument is 1 by default, if you want to create balloons repeatedly, use;

gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )

You can also remove the object from your table in popBalloon function. First create an index counter globally;

local index = 1

Give your index to balloon and also increment it;

newBalloon.index = index
index = index + 1

Now back to your popBalloon function, you can see tapped balloon's index by tappedBalloon.index so you can manage your table there.