AddEventListener:监听器不能为零

时间:2017-03-20 17:04:42

标签: lua corona

有人可以解释这里出了什么问题吗?一添加AddEventListener

就出错了
   newBalloon:addEventListener( "tap", pushBalloon )

完整代码:

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

谢谢。

2 个答案:

答案 0 :(得分:2)

在添加事件侦听器时,您所指的函数(pushBalloon)尚不存在。所以把函数的定义放在行上面你就像那样添加事件监听器

 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 )

答案 1 :(得分:0)

在您要添加侦听器的文件中,未定义pushBalloon()。您应该在文件的前面添加一个前向声明:

local scene = ...
local pushBalloon

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

pushBalloon = function ( event )
-- your function code