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. so how can i say if there is 200px between the objects spawn a new one.

also if i try to remove theobjects “onComplete” it removes the new objects to is there a simple fix for this ? 

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 )

local function distBetween( point1, point2 )
        local xFactor = point2.x-point1.x ; local yFactor = point2.y-point1.y
        local dist = math_sqrt((xFactor*xFactor) + (yFactor*yFactor))
        return dist
end

Pass in the two objects you want to know the distance between.

Rob

local function distBetween( point1, point2 )
        local xFactor = point2.x-point1.x ; local yFactor = point2.y-point1.y
        local dist = math_sqrt((xFactor*xFactor) + (yFactor*yFactor))
        return dist
end

Pass in the two objects you want to know the distance between.

Rob