2013-02-28 46 views
0

當我嘗試與BCloud1.png牀碰撞時,我試圖索引全局'BCoud1'(一個零值)?有沒有簡單的方法來解決這個錯誤?碰撞嘗試索引全局(一個零值)

我已經做到了這一點:

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) 
+0

檢查參數'physics.addBody'。 BCloud1以概率1/3進行初始化。 – 2013-02-28 15:08:29

+0

是的,但如果它是1我想添加physics.addBody(BCloud1, – 2013-02-28 15:45:01

+0

)您應該使用BCloud1框架操作'如果BCloud1然後...結束' – 2013-02-28 15:49:39

回答

1

你在哪裏創建一個名爲BCloud1的顯示對象?

在您的代碼:

BCloud1.collision = onLocalCollision1 
BCloud1:addEventListener("collision", BCloud1.png) 

BCloud1必須存在,你的發佈代碼永遠不會創建此。我懷疑它從你的getRandomStar()函數開始。你永遠不會返回創建的對象。但是,在上面的eventListener設置中,您正在將BCloud1.png視爲名爲BCloud1的表,並使用名爲.png的成員查看它,但BCloud1.png聽起來像圖像的文件名。也許你失去了一些東西,如:

BCloud1 = display.newImage( 「BCloud1.png」)

相關問題