Lua 要一张桌子吗?

Lua 要一张桌子吗?,lua,coronasdk,Lua,Coronasdk,我昨天开始学习电晕,很抱歉我的无知。有人能解释一下为什么第108行是“期望表”(physics.addBody(newballon,“dynamic”{radius=70,bounce=0.8}))。我想要它做的就是从对象表中获取图像并显示它 local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start()

我昨天开始学习电晕,很抱歉我的无知。有人能解释一下为什么第108行是“期望表”(physics.addBody(newballon,“dynamic”{radius=70,bounce=0.8}))。我想要它做的就是从对象表中获取图像并显示它

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
。另外,
mainGroup==nil
将其作为父组传递给
display.newImageRect()
。由于这些原因,
newballon
当您将其传递给
physics.addBody()
时,它可能是
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