期待 table?

Expecting a table?

我昨天开始学习 Corona,很抱歉我的无知。有人可以解释为什么它在第 108 行是 "expecting table" (physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } ))。我想让它做的就是从对象 sheet 中获取图像并显示它。

local composer = require( "composer" )

local scene = composer.newScene()


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



-- Configure image sheet
local sheetOptions =
{
    frames =
    {
        {   
            x = 0,
            y = 0,
            width = 112,
            height = 142
        },
        {   
            x = 0,
            y = 142,
            width = 112,
            height = 284
        },
        {   
            x = 0,
            y = 284,
            width = 112,
            height = 568
        },
        {  
            x = 0,
            y = 568,
            width = 112,
            height = 710
        },
        {  
            x = 0,
            y = 710,
            width = 112,
            height = 852
        },
         {  
            x = 0,
            y = 852,
            width = 112,
            height = 994
        },
         {  
            x = 0,
            y = 994,
            width = 112,
            height = 1136
        },
         {  
            x = 0,
            y = 1136,
            width = 112,
            height = 1278
        },
         {  
            x = 0,
            y = 1278,
            width = 112,
            height = 1420
        },



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






local tapCount = 0
local platform
local balloon
local balloon2
local balloon3
local tapText




local function createBalloon()

    newBalloon = display.newImageRect( mainGroup, objectSheet, 1, 112, 142 )

    physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )
    newBalloon.myName = "Bigballoon"

    local whereFrom = math.random( 3 )

    if ( whereFrom == 1 ) then
        -- From the left
        newBalloon.x = 30
        newBalloon.y = math.random( display.contentHeight )

    elseif ( whereFrom == 2 ) then
        -- From the top
        newBalloon.x = 60
        newBalloon.y =  math.random( display.contentHeight )
    elseif ( whereFrom == 3 ) then
        -- From the right
        newBalloon.x = 90
        newBalloon.y =  math.random( display.contentHeight )

end
end


local function pushBalloon()
    --  balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
    -- tapCount = tapCount + 1
    -- tapText.text = tapCount
     balloon.gravityScale = 10
     balloon:applyLinearImpulse( 0.1, 0, balloon.x, balloon.y )
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
local platform = display.newImageRect( "platform.png", 300, 50 )
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
balloon = display.newImageRect( "balloon.png", 112, 112 )
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8

balloon2 = display.newImageRect( "balloon.png", 112, 112 )
balloon2.x = display.contentCenterX-100
balloon2.y = display.contentCenterY
balloon2.alpha = 0.8

balloon3 = display.newImageRect( "balloon.png", 112, 112 )
balloon3.x = display.contentCenterX+100
balloon3.y = display.contentCenterY
balloon3.alpha = 0.8


createBalloon()

tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )
tapText:setFillColor( 0, 0, 0 )


physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon2, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon3, "dynamic", { radius=50, bounce=0.6 } )


balloon:addEventListener( "tap", pushBalloon )
balloon.gravityScale = 0
balloon:setLinearVelocity( 0, -10 )

balloon2:addEventListener( "tap", pushBalloon2 )
balloon2.gravityScale = 0
balloon2:setLinearVelocity( 0, -10 )

balloon3:addEventListener( "tap", pushBalloon3 )
balloon3.gravityScale = 0
balloon3:setLinearVelocity( 0, -10 )

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
        -- 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

createBalloon()中初始化时,应将newBalloon声明为local。此外,当您将其作为父组传递给 display.newImageRect() 时,mainGroup == nil。由于这些原因,当您将 newBalloon 传递给 physics.addBody() 而不是 DisplayObject 时,newBalloon 可能是 nil,而 DisplayObject 正是该方法要求的第一个参数。

为了安全起见,在createBalloon(),您可以这样做:

local newBalloon = display.newImageRect( ... )
if newBalloon then
    physics.addBody( newBalloon, ...  )
else
    print("WARNING: newBalloon is nil in createBalloon")
end

顺便说一下,如果您希望所有气球都在同一个 GroupObject 中,您可以添加:

local balloonGroup -- forward declaration

...

local function createBalloon()

    local newBalloon = display.newImageRect( balloonGroup, ... )

    ...

end


local scene:create()

    ...

    balloonGroup = display.newGroup()
    sceneGroup:insert( balloonGroup )
    balloonGroup:toFront()

    ...

    balloonGroup:insert( balloon )
    balloonGroup:insert( balloon2 )

    ...

end