reappearing objects - location

I am working on an app where I have a little robot chasing a flower on a grid. When the robot catches up to the flower the player gets a point, the flower disappears and reappears somewhere else on the grid. My problem is that sometimes the flower reappears in the same location it disappeared from. (I’m using math.random for the coordinates). Can anyone point me in the direction of making sure the flower can only reappear at least 50 pixels away from it’s location when it disappeared?

Thanks [import]uid: 48122 topic_id: 19942 reply_id: 319942[/import]

This could maybe work…

spawnPointX = math.random (player.x + 50)
spawnPointY = math.random (player.y + 50)
Regards, Joakim
[import]uid: 81188 topic_id: 19942 reply_id: 77603[/import]

off the top of my head: (so you may need to tweak it a bit, I have just typed this straight into the web browser as Im not near my dev machine)

[code]
function tooClose(x1,y1,x2,y2,tolerance)

if math.abs(x1-x2) < tolerance or math.abs(y1-y2) < tolerance then
return true
else
return false
end

end

–pseudo code

–store last known position
lastx = sprite.x
lasty = sprite.y

–think of a new spot
sprite.x = randomvalue
sprite.y = randomvalue

–if too close, have another try
–shouldnt take more than 2 shots to get something usable
–99% of the time

while tooClose(sprite.x,sprite.y,lastx,lasty,50)
sprite.x = randomvalue
sprite.y = randomvalue
end

[/code] [import]uid: 108660 topic_id: 19942 reply_id: 77606[/import]

Thanks! I’ll try both ways and use the one I like better. Thanks Y’all!! [import]uid: 48122 topic_id: 19942 reply_id: 77613[/import]