简体   繁体   English

在电晕中,物理学不适用于一组对象

[英]In Corona physics not working for a group of objects

I have a game where eggs with numbers on them drop and there is a basket with number at the bottom. 我有一个游戏,上面有数字的鸡蛋掉落,底部有一个数字篮子。 The egg and the number are in a group and added as a physics body. 鸡蛋和数字在一个组中,并作为物理体添加。 the same for the basket and number. 篮子和编号相同。 when only the egg is added as a physics body and not the egggroup, then the physics work. 当仅将鸡蛋添加为物理物体而不添加鸡蛋组时,则该物理过程起作用。 The following is my code 以下是我的代码

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")

if in the physics.addBody(eggGroup , {bounce=0.2}) I change to add only egg, then the physics work. 如果在physics.addBody(eggGroup,{bounce = 0.2})中,我更改为仅添加鸡蛋,则物理工作正常。 Please tell me how to fix this!!! 请告诉我如何解决这个问题!!!

There are several ways of fixing it but the best/easiest one would be: 有几种修复方法,但是最好/最简单的方法是:

Add psyhics to egg only. 仅在鸡蛋中添加心理疗法。

Change : 变更:

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

to (so when multiple object will be there its much easier to retrieve reference to text on egg) 到(因此,当有多个对象时,更容易检索到鸡蛋上的文本的引用)

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

Then add something like this. 然后添加这样的东西。

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

 Runtime:addEventListener( "enterFrame", adjustElements )

This will add another function that will make text follow Your egg. 这将添加另一个功能,使文本跟随您的卵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM