Lua 测量生成的对象之间的距离
如何测量生成的对象之间的距离? 我正在使用计时器。performWithDelay来计时对象,但是如果你重新启动游戏几次,它就会出错。那么,我如何判断对象之间是否存在200px,从而生成一个新对象呢 此外,如果我尝试删除对象“onComplete”,它会删除新对象,是否有简单的修复方法Lua 测量生成的对象之间的距离,lua,coronasdk,Lua,Coronasdk,如何测量生成的对象之间的距离? 我正在使用计时器。performWithDelay来计时对象,但是如果你重新启动游戏几次,它就会出错。那么,我如何判断对象之间是否存在200px,从而生成一个新对象呢 此外,如果我尝试删除对象“onComplete”,它会删除新对象,是否有简单的修复方法 holl:removeSelf() holl = nil 繁殖代码: function hollspawn() screenGroup = self.view holl = display.newRec
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 )
要得到两个物体之间的距离,可以使用毕达哥拉斯定理
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
像这样使用它:
local newThing01 = spawnNewObject()
group:insert(newThing01)
local newThing02 = spawnNewObject()
group:insert(newThing02)
这样,如果您删除newThing01,它应该保持newThing02不变,因为它们是一个单独的实例(彼此独立)这取决于您如何访问对象,您的帖子没有足够的信息。此外,还不清楚您要做什么:只有当两个其他特定对象的间距超过200 px,或者所有对象中的任何两个对象的间距超过200 px时,您才尝试让hollspawn生成一个对象?
local newThing01 = spawnNewObject()
group:insert(newThing01)
local newThing02 = spawnNewObject()
group:insert(newThing02)