Search code examples
luacoronasdk

Listen Event for Any Object in a Table


Another newbie query. Now on my third day of working with Corona.

The following code works fine: Balloons are generated and float into the air. Now I want to use :addEventListener( "tap", pushBalloon ) to make it so that when a balloon is clicked the pushBalloon is executed. Can anyone tell me what variable I would use and how I would define it? And also I guess I would have to change the pushBalloon function too for the new variable.

Thank you.

local function createBalloon()
local randomBalloon = math.random( 10 )


    local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )
    table.insert( balloonsTable, newBalloon )
    physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
    newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150

local whereFrom = math.random( 3 )

    if ( whereFrom == 1 ) then
        -- From the left
        newBalloon.x = 100
        newBalloon.y = display.contentHeight+150

    elseif ( whereFrom == 2 ) then
        -- From the top
        newBalloon.x = 160
        newBalloon.y = display.contentHeight+150
    elseif ( whereFrom == 3 ) then
        -- From the right
        newBalloon.x = 220
        newBalloon.y = display.contentHeight+150

end
end

local function gameLoop()
    -- Create new balloon
    createBalloon()
     -- Remove balloons which have drifted off screen
    for i = #balloonsTable, 1, -1 do
     local thisBalloon = balloonsTable[i]

        if ( thisBalloon.x < -100 or
             thisBalloon.x > display.contentWidth + 100 or
             thisBalloon.y < -100  )
        then
            display.remove( thisBalloon )
            table.remove( balloonsTable, i )
        end

    end

end



local function pushBalloon()
    --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
    -- tapCount = tapCount + 1
    -- tapText.text = tapCount
     newBalloon.gravityScale = 10

end

Solution

  • You are adding the newBalloon objects to a table, but you should add the event listener to each newBalloon DisplayObject as it is instantiated. This doesn't do exactly what you ask in the title (where simply inserting an object into a table would effectively add an event listener to that object), but achieves the event response I think you are looking for.

    If you are tapping the balloon, you would put the listener on the balloon. If you use a "tap" event, the target property tells you which object was touched, so your pushBalloon() function works for any balloon.

    local pushBalloon( event )
       local balloon = event.target
    
       if event.phase == "began"
    
           -- do something to the balloon object (apply impulse, etc.)
    
       end
    
    end
    
    
    local function createBalloon()
        ...
        local newBalloon = display.newImageRect( ... )
    
        if newBalloon then
    
            -- set properties of DisplayObject and add event listener
    
            newBallon:addEventListener( "tap", pushBalloon )
    
        end
        ...
    end
    

    I have wrapped the call to addEventListener() in a check to make sure newBalloon ~= nil.