Lua 冲突尝试索引全局(零值)

Lua 冲突尝试索引全局(零值),lua,coronasdk,Lua,Coronasdk,当我尝试与BCloud1.png bed冲突时,我会尝试索引全局“BCoud1”(一个nil值)?有没有简单的方法来解决这个错误 我已经这样做了: function getRandomStar() local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable local randomStar = display.newImage(temp.imgpath) -- Set

当我尝试与BCloud1.png bed冲突时,我会尝试索引全局“BCoud1”(一个nil值)?有没有简单的方法来解决这个错误

我已经这样做了:

function getRandomStar()
    local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
    local randomStar = display.newImage(temp.imgpath) -- Set image path for object

    if ( temp.imgpath == "BCloud1.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( BCloud1, { density=3.0, friction=0.5, bounce=0.3 } )
    end

    if ( temp.imgpath == "BCloud2.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    if ( temp.imgpath == "BCloud3.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    randomStar.myName = "star" -- Set the name of the object to star
    randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
    randomStar.x = math.random(-30, _W);    
    randomStar.y = -35;
    randomStar.rotation = math.random(0,20) -- Rotate the object

    starMove = transition.to(randomStar, {
        time=randomStar.movementSpeed, 
        y=500,
        onComplete = function(self) self.parent:remove(self); self = nil; 
        end
        }) -- Move the star
end--END getRandomStar()


local function onLocalCollision1( self, event )
    if ( event.phase == "began" ) then
print (" Collision1 ")
    elseif ( event.phase == "ended" ) then
    end
end
BCloud1.collision = onLocalCollision1
BCloud1:addEventListener( "collision", BCloud1.png)

在何处创建名为BCloud1的显示对象

在代码中:

BCloud1.collision = onLocalCollision1
BCloud1:addEventListener( "collision", BCloud1.png)
BCloud1需要存在,而您发布的代码永远不会创建它。我怀疑它是从getRandomStar()函数开始的。您永远不会返回创建的对象。但是,在上面的eventListener设置中,您将BCloud1.png视为一个名为BCloud1的表,其中的成员名为.png,尽管看着它,BCloud1.png听起来像是图像的文件名。也许你错过了一些东西,比如:


BCloud1=display.newImage(“BCloud1.png”)

检查物理.addBody的参数。BCloud1以1/3的概率初始化。是的,但如果它是1,我想添加physics.addBody(BCloud1,您应该用
来框接BCloud1的操作,如果是BCloud1,那么…结束