2017-03-19 91 views
2

昨天我開始學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

,當你在createBalloon()初始化,您應該聲明newBalloonlocal。另外,當您將其作爲父組傳遞給display.newImageRect()時,您還需要mainGroup == nil。由於這些原因,當您將它傳遞給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 
相關問題