Spawn objects on all screen borders

First of all hello to every one :slight_smile:

This is my first post in the forum and i think its too foolish.

I am making (learning corona) tower defense game in which my tower is located in the center of the screen, and i want to spawn enemies off screen and translate them all towards tower. I am able to handle this translation part and spawning enemies at any one axis.

FOR eg.
object.x = math.random(0,_W); object.y = 0 (in this case enemies will spawn only at the top side of the screen)

but m stuck at how to spawn them all around the tower? (from all the sides)

I hope m posting this in right place.
Thanks in advance [import]uid: 83799 topic_id: 25034 reply_id: 325034[/import]

Like most programming puzzles, there are multiple solutions. This might be one.

[lua]_LEFT = 0
_RIGHT = display.contentWidth
_TOP = 0
_BOTTOM = display.contentHeight

sides = { _TOP, _BOTTOM }

object.x = math.random(0,_W)
object.y = sides[math.random(2)][/lua]

Or at least kickstart some ideas to riff on.

A more complete solution might look something like this.

[lua]_LEFT = 0
_RIGHT = display.contentWidth
_TOP = 0
_BOTTOM = display.contentHeight

if math.random(100) < 51 then
local sides = { _TOP, _BOTTOM }
object.x = math.random(0,_W)
object.y = sides[math.random(2)]
else
local sides = { _LEFT, _RIGHT }
object.x = sides[math.random(2)]
object.y = math.random(0,_H)
end[/lua]

[import]uid: 44647 topic_id: 25034 reply_id: 101698[/import]

----------------------------------------------------------------------------------------   
 -- Get random position for where the enemy is spawned from and random velocity   
----------------------------------------------------------------------------------------  
local function getRandomXY()  
 -- first select which location mouse it to start at ( top =1, bottom=2, left=3, right=4 )  
 local randomSide = math.random( 1, 4 )  
  
 local left,top -- seems easier than x/y  
  
 --Depending on the random generation ( x/y become reversied for position starting lol )  
  
 if randomSide ==1 then --Top  
 top = 0  
 left = math.random( 0, \_w )  
 elseif randomSide ==2 then --bottom  
 top = \_h  
 left = math.random( 0, \_w )  
  
 elseif randomSide ==3 then --left  
 top = math.random( 0, \_h )  
 left = 0  
  
 elseif randomSide ==4 then --right  
 top = math.random( 0, \_h )  
 left = \_w  
 end  
  
 local velocity = math.random( 2100, 2600 ) --set random enemy velocity  
  
 return left, top, velocity, randomSide  
end  

And call it like this

x,y, velocity, iDirection = getRandomXY() -- returns random settings for enemy spawning  

I use this code for generating random positions in my App Sassys Easter Adventure. The direction is used so you can also change which direction your image needs to be pointing. ( to the left up down etc )
[import]uid: 11860 topic_id: 25034 reply_id: 101701[/import]

Hey thanks guys for the replies…
Thanks doubleslashdesign solution worked :slight_smile: (most of the time)

But some time m getting object spawn in middle of the screen its happening randomly. some times its working perfectly fine and sometimes its not. Any idea…? Here is my code may be m missing something.

_W = display.contentWidth
_H = display.contentHeight

local function gerRandomxy()
local randomSide = math.random(1, 4)
local xpos,ypos

if randomSide == 1 then
xpos = math.random(0, _W)
ypos = 0
elseif randomSide == 2 then
xpos = 0
ypos = math.random(0, _H)
elseif randomSide == 3 then
xpos = _W
ypos = math.random(0, _H)
elseif randomSide == 4 then
xpos = math.random(0, _W)
ypos = _H
end
return xpos,ypos
end

local function spawnrect ()
local rect = display.newRect(0,0,50,50)
rect:setReferencePoint(display.CenterReferencePoint)
rect.x = gerRandomxy()
rect.y = gerRandomxy()
print (rect.x)
print (rect.y)
end

tmr = timer.performWithDelay(1000,spawnrect,15) [import]uid: 83799 topic_id: 25034 reply_id: 101768[/import]

Instead of

[lua]rect.x = gerRandomxy()
rect.y = gerRandomxy()[/lua]

Do it with a single call to your function, which returns both x and y values:

[lua]rect.x, rect.y = gerRandomxy()[/lua] [import]uid: 44647 topic_id: 25034 reply_id: 101790[/import]

Thanks toby2… it works fine now

I must say you are too quick replying back on query’s…
Thank you very much.
[import]uid: 83799 topic_id: 25034 reply_id: 101793[/import]

@siddhesh.meher, Sorry for the late reply.

I’ve been at work, and at home I am installing a new Quad Core Desktop and been involved with a Loading frenzy of software… You know new toys and all.

@toby2, thanks for pointing out the fix :slight_smile:

Hope everything else is good to go.

Larry [import]uid: 11860 topic_id: 25034 reply_id: 101940[/import]