期待一张桌子?

时间:2017-03-19 18:17:31

标签: lua corona

我昨天开始学习电晕,对我的无知感到抱歉。有人可以解释为什么它是"期待桌子"在第108行(physics.addBody(newBalloon," 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

1 个答案:

答案 0 :(得分:1)

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

为安全起见,在local newBalloon = display.newImageRect( ... ) if newBalloon then physics.addBody( newBalloon, ... ) else print("WARNING: newBalloon is nil in createBalloon") end 中,你可以这样做:

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

顺便说一句,如果你想让所有的气球都在同一个GroupObject中,你可以添加它:

tree lab = build_decl (gimple_location (gsi_stmt (gsi)),
LABEL_DECL, NULL_TREE, void_type_node);
DECL_ARTIFICIAL (lab) = 0;
DECL_IGNORED_P (lab) = 1;
DECL_CONTEXT (lab) = current_function_decl;
DECL_NAME(lab) = get_identifier("test_text");

location_t loc = gimple_location (gsi_stmt (gsi));
tree label = create_artificial_label (loc);
gsi_insert_before (&gsi, gimple_build_label (lab), GSI_SAME_STMT);