简体   繁体   English

测量产生的物体之间的距离

[英]measure distance between spawned objects

How can i measure distance between spawned objects? 如何测量产生的物体之间的距离? Im using a timer.performWithDelay to time the objects but if you restart the game a couple of times it messes up. 我使用timer.performWithDelay来给对象计时,但是如果您重启游戏几次,就会搞砸了。 so how can i say if there is 200px between the objects spawn a new one. 因此,我该如何说对象之间是否有200px生成一个新对象呢?

also if i try to remove the objects "onComplete" it removes the new objects to is there a simple fix for this ? 如果我尝试删除对象“ onComplete”,它也会删除新对象,对此是否有简单的解决方法?

holl:removeSelf()
holl = nil

spawn code: 产生代码:

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 )

To get the distance between 2 objects you can use Pythagoras theorem 要获得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

To check for a distance less than 200 between "a" and "b" you can do: 要检查“ a”和“ b”之间的距离小于200,可以执行以下操作:

if ( getDistance(a,b) < 200 ) then
  --Your code here
end

***Note that the distance is not in pixels, but is a measure in relation to the contentWidth and contentHeight that you chose for your Corona Settings. ***请注意,距离不是以像素为单位,而是相对于您为电晕设置选择的contentWidth和contentHeight的度量。

You can have some trouble with your spawn function because it does not create a new instance each time its called, possibly reusing the same objects over and over. 您可能会在使用spawn函数时遇到麻烦,因为它不会在每次调用时都创建一个新实例,这可能会反复使用相同的对象。 Try something like this as a factory pattern: 尝试将这种方式作为工厂模式:

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

Use it like this: 像这样使用它:

local newThing01 = spawnNewObject()
group:insert(newThing01)

local newThing02 = spawnNewObject()
group:insert(newThing02)

This way if you remove newThing01, it should keep newThing02 untouched because they are a separate instance (independent from each other) 这样,如果删除newThing01,则应该保持newThing02不变,因为它们是一个单独的实例(彼此独立)

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

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