错误的参数#-1到'addBody'(代理预期,得到零)

时间:2014-10-13 16:03:01

标签: lua corona

我正在使用此代码生成对象但有时我得到此错误(错误的参数#-1到'addBody'(代理预期,得到零))任何想法为什么?

local numberRock = 1

local function clearRock( thisRock )
   display.remove( thisRock ) ; thisRock = nil
end

local function spawnRocks()
    rockChek = math.random(1, 7)
    if  rockChek == 1 then
        rocks = display.newImageRect("rockes/rock1.png",110 ,86 )
    elseif  rockChek == 2 then
        rocks = display.newImageRect("rockes/rock2.png",97 ,65 )
    elseif  rockChek == 3 then
        rocks = display.newImageRect("rockes/rock3.png",101 ,61 )
    elseif  rockChek == 6 then
        rocks = display.newImageRect("rockes/rock4.png",95 ,59 )
    elseif  rockChek == 7 then
        rocks = display.newImageRect("rockes/rock5.png",107 ,77 )
    end   
    for i=1,numberRock do
        rocks.x = math.random(-10, 400);
        rocks.y =display.contentHeight + 100;
        transition.to( rocks, { time=2000,  y= display.contentHeight - display.contentHeight - 100 , onComplete=clearRock  } );
        physics.addBody( rocks, "dynamic", {density=.1, bounce=0.1, friction=.2, shape= shape2 ,filter=playerCollisionFilter } )   
        rocks.name = "rocks"
        rocks.isSensor = true
        rocks.rotation =  math.random(100, 1800)
   end
end
Rockspawner1 = timer.performWithDelay( 1500, spawnRocks, 0 )  

2 个答案:

答案 0 :(得分:2)

你忘了处理rockCheck为4或5的情况。加上那些,你会没事的。

或者,让ramdom调用从1到5,并分别用4和5替换6和7。

顺便说一下,真的考虑让rockCheck成为local变量(local rockCheck = ...

答案 1 :(得分:1)

rocks的随机值为4或5

时,

nil位于下面的rockCheck

physics.addBody( rocks, "dynamic", {density=.1, bounce=0.1, friction=.2, shape= shape2 ,filter=playerCollisionFilter } )