2017-03-19 16 views
2

私は昨日コロナを学び始めたので、私の無知には申し訳ありません。誰かがなぜ108行目の "expecting table"(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()でそれを初期化するときは、localとしてnewBalloonを宣言する必要があります。また、親グループとしてdisplay.newImageRect()に渡すと、mainGroup == nilになります。これらの理由から、newBalloonphysics.addBody()に渡すとnilになる可能性があります。これは、そのメソッドが最初の引数として必要とするDisplayObjectではなくなります。安全のために

createBalloon()で、あなたがこれを行うことができます:

:あなたはすべての風船が同じGroupObjectになりたい場合は、これを追加することができ、ちなみに

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 
関連する問題