Atrag
Atrag

Reputation: 753

AddEventListener: Listener cannot be nil

Can someone please explain what is going wrong here? It went wrong as soon as I added the AddEventListener

   newBalloon:addEventListener( "tap", pushBalloon )

Complete Code:

local composer = require( "composer" )

local scene = composer.newScene()


local physics = require( "physics" )
physics.start()



-- Configure image sheet
local positioninsheetOptions = 144.1
local sheetOptions =
{
    frames =
    {
        {   
            x = 0,
            y = 0,
            width = 112,
            height = 142
        },
        {   
            x = 0,
            y = positioninsheetOptions,
            width = 112,
            height = 142
        },
        {   
            x = 0,
            y = positioninsheetOptions*2,
            width = 112,
            height = 142
        },
        {  
            x = 0,
            y = positioninsheetOptions*3,
            width = 112,
            height = 142
        },
        {  
            x = 0,
            y = positioninsheetOptions*4,
            width = 112,
            height = 142
        },
         {  
            x = 0,
            y = positioninsheetOptions*5,
            width = 112,
            height = 142
        },
         {  
            x = 0,
            y = positioninsheetOptions*6,
            width = 112,
            height = 142
        },
         {  
            x = 0,
            y = positioninsheetOptions*7,
            width = 112,
            height = 142
        },
         {  
            x = 0,
            y = positioninsheetOptions*8,
            width = 112,
            height = 142
        },

         {  
            x = 0,
            y = positioninsheetOptions*9,
            width = 112,
            height = 142
        },

             {  
            x = 0,
            y = positioninsheetOptions*10,
            width = 112,
            height = 142
        },




    },
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )






local tapCount = 0
local platform

local tapText
local balloonsTable = {}
local leftBorder
local rightBorder

local backGroup
local mainGroup
local uiGroup

local platform
local platform2




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


    local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )

     if newBalloon then


        table.insert( balloonsTable, newBalloon )
    physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
    newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150
        newBalloon:addEventListener( "tap", pushBalloon )

    end


local whereFrom = math.random( 3 )

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

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

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( event )
local tappedBalloon = event.target
    --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
    -- tapCount = tapCount + 1
    -- tapText.text = tapCount 
    if event.phase == "began" then
     tappedBalloon.gravityScale = 10
     end

end


local function pushBalloon2()
    --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
    -- tapCount = tapCount + 1
    -- tapText.text = tapCount
     balloon2.gravityScale = 10
     balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end


local function pushBalloon3()
    --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
    -- tapCount = tapCount + 1
    -- tapText.text = tapCount
     balloon3.gravityScale = 10
     balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end



-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()
function scene:create( event )

    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY



 platform = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform.x = -5
platform.y = 0
physics.addBody( platform, "static", { friction=0.5, bounce=0.3 } )

 platform2 = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform2.x = display.contentWidth+5
platform2.y = 0
physics.addBody( platform2, "static", { friction=0.5, bounce=0.3 } )


createBalloon()




end


-- show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then

        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
gameLoopTimer = timer.performWithDelay( 1250, gameLoop, 0 )

        -- Code here runs when the scene is entirely on screen
physics.start()

    end
end


-- hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen
physics.pause()
    end
end


-- destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene

Thank you.

Upvotes: 2

Views: 1285

Answers (2)

ldurniat
ldurniat

Reputation: 1702

You refer to function (pushBalloon) do not exist yet when add event listener. So put definition of function above line you add event listener like that

 local function pushBalloon( event )
    local tappedBalloon = event.target
        --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
        -- tapCount = tapCount + 1
        -- tapText.text = tapCount 
        if event.phase == "began" then
         tappedBalloon.gravityScale = 10
         end

 end

    ...

 newBalloon:addEventListener( "tap", pushBalloon )

Upvotes: 3

GoojajiGreg
GoojajiGreg

Reputation: 1193

At the point in the file where you are adding the listener, pushBalloon() is not defined. You should add a forward declaration earlier in the file:

local scene = ...
local pushBalloon

...
-- createBalloon() and other existing code
...

pushBalloon = function ( event )
-- your function code

end

Upvotes: 0

Related Questions