2015-12-02 29 views
0

我有一個遊戲,其中有數字的雞蛋下降,並在底部有一個編號的籃子。雞蛋和數量是在一個組中,並添加爲一個物理機構。籃子和號碼相同。當只有雞蛋被添加爲物理體而不是egggroup時,則是物理工作。下面是我的代碼在電暈物理不適用於一組物體

local physics =require("physics") 
physics.start() 
physics.setGravity(0,9.8) 

local egg=display.newImage("egg.jpg") 
egg.numberValue=myRandomNumber 

local eggText = display.newText({x = egg.x, y = egg.y, text = tostring(egg.numberValue), fontSize = 30, font = native.systemFontBold }) 

local eggGroup = display.newGroup() 
eggGroup :insert(egg) 
eggGroup :insert(eggText) 

physics.addBody(eggGroup , {bounce=0.2}) --if I change to add only egg, then the physics work 

-- add basketgroup 
local basket=display.newImage("basket.png") 
basket.numberValue=math.random(10,20) 
local basketText = display.newText({x = basket.x, y = basket.y, text = tostring(basket.numberValue), fontSize = 30, font = native.systemFontBold })  

local basketGroup = display.newGroup() 
basketGroup :insert(basket) 
basketGroup :insert(basketText) 

physics.addBody(basket ,"static") 

如果在physics.addBody(eggGroup,{反彈= 0.2})我更改爲只添加雞蛋,那麼物理工作。請告訴我如何解決這個問題!

回答

0

有固定,但最好/最簡單的一個會的幾種方法:

添加psyhics只雞蛋。

變化:

local eggText = display.newText({x = egg.x, y = egg.y, text = tostring(egg.numberValue), fontSize = 30, font = native.systemFontBold }) 

到(所以當多個對象將在那裏它更容易檢索參考雞蛋文本)

egg.eggText = display.newText({x = egg.x, y = egg.y, text = tostring(egg.numberValue), fontSize = 30, font = native.systemFontBold }) 

然後加入這樣的事情。

local function adjustElements() 
egg.eggText.x = egg.x 
egg.eggText.y = egg.y 
egg.eggText.rotation = egg.rotation 
end 

Runtime:addEventListener("enterFrame", adjustElements) 

這將添加另一個功能,使文本跟隨你的蛋。

相關問題