如何防止随机产生的对象重叠?电晕sdk

时间:2013-08-12 01:33:24

标签: sdk corona

[CODE]

local buildingsA= getBuildings("level1a")
local buildingsB= getBuildings("level1b")
local buildingsC= getBuildings("level1c")

buildingsA.x=1100
buildingsB.x=buildingsA.x+buildingsA.width+300
buildingsC.x=buildingsB.x+buildingsB.width+350
--END OF LEVEL1 CREATION

timer.performWithDelay(1, function(e)
    buildingsA.x = buildingsA.x -15
    buildingsB.x = buildingsB.x-15
    buildingsC.x = buildingsC.x-15
    end, 0 )    

function scrollblocks(self, event)
    if self.x <-100 then
        self.x=math.random(1200,2000) 
    end
end
buildingsA.enterFrame= scrollblocks
buildingsB.enterFrame= scrollblocks
buildingsC.enterFrame= scrollblocks

Runtime:addEventListener("enterFrame", buildingsA)
Runtime:addEventListener("enterFrame", buildingsB)
Runtime:addEventListener("enterFrame", buildingsC)

--Add hole
local hole = display.newImage("hole.png", true)
    physics.addBody(hole, "static", {friction= 0.5, bounce=0})
    hole.y=floor_bottom.y-60
    hole.x= buildingsA.x+100

timer.performWithDelay(1, function(e) 
    hole.x= hole.x-15 
    end, 0 )    

hole.enterFrame=scrollblocks
Runtime:addEventListener("enterFrame", hole)

[/ CODE]

我首先制作了建筑物,然后慢慢地向左移动,并从屏幕右侧产生随机的建筑物,以便显示为环状,建筑物从左侧出现并从右侧出现。 我对洞也这样做,但它们似乎与建筑物重叠。 如何限制它们重叠?

1 个答案:

答案 0 :(得分:0)

您可以使用object:toFront()object:toBack()更改显示层次结构,也可以通过将索引编号设置为显示组来更改它,例如:

 displayGroup = display.newGroup()
 object1 = display.newImage("img1.png")
 object2 = display.newImage("img2.png")
 object3 = display.newImage("img3.png")

--inserting object to display group using index
 group:insert(1,object1)
 group:insert(2,object2)
 group:insert(3,object3)