[英]measure distance between spawned objects
如何测量产生的物体之间的距离? 我使用timer.performWithDelay来给对象计时,但是如果您重启游戏几次,就会搞砸了。 因此,我该如何说对象之间是否有200px生成一个新对象呢?
如果我尝试删除对象“ onComplete”,它也会删除新对象,对此是否有简单的解决方法?
holl:removeSelf()
holl = nil
产生代码:
function hollspawn()
screenGroup = self.view
holl = display.newRect( 0, 0, math.random(10, 500), 53 )
holl.y = display.contentHeight - holl.contentHeight/2
holl.x =display.contentWidth + holl.contentWidth/2
holl:setFillColor( 1, 0, 0 )
holl.name = "hollgameover"
physics.addBody(holl, "static", {density=.1, bounce=0.5, friction=.2,filter=playerCollisionFilter } )
screenGroup:insert(holl)
trans55 = transition.to(holl,{time=2000, x=display.contentWidth - display.contentWidth - holl.contentWidth/2 - 20, onComplete=pluspoint, transition=easing.OutExpo } ) --onComplete=jetReady
end
timereholl = timer.performWithDelay( 1500 , hollspawn, 0 )
要获得2个对象之间的距离,可以使用毕达哥拉斯定理
function getDistance(objA, objB)
-- Get the length for each of the components x and y
local xDist = objB.x - objA.x
local yDist = objB.y - objA.y
return math.sqrt( (xDist ^ 2) + (yDist ^ 2) )
end
要检查“ a”和“ b”之间的距离小于200,可以执行以下操作:
if ( getDistance(a,b) < 200 ) then
--Your code here
end
***请注意,距离不是以像素为单位,而是相对于您为电晕设置选择的contentWidth和contentHeight的度量。
您可能会在使用spawn函数时遇到麻烦,因为它不会在每次调用时都创建一个新实例,这可能会反复使用相同的对象。 尝试将这种方式作为工厂模式:
function spawnNewObject()
local newInstance = display.newRect( 0, 0, math.random(10, 500), 53 )
-- Do things to newInstance here
newInstance.y = display.contentHeight - (newInstance.contentHeight * 0.5)
newInstance.x = display.contentWidth + (newInstance.contentWidth * 0.5)
newInstance:setFillColor( 1, 0, 0 )
newInstance.name = "hollgameover"
-- Return a new instanced object
return newInstance
end
像这样使用它:
local newThing01 = spawnNewObject()
group:insert(newThing01)
local newThing02 = spawnNewObject()
group:insert(newThing02)
这样,如果删除newThing01,则应该保持newThing02不变,因为它们是一个单独的实例(彼此独立)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.