# Actor spawning confusion...

I’m messing around with an idea for a game similar to whack a mole. I want to spawn an ellipse every second in one of 4 designated spots, and want the ellipses to be destroyed when touched or after 1 second. I know that I will need to have a table to keep track of the ellipses, and a function to spawn the ellipses, but I am unsure of how to make the ellipse spawn in a random one of 4 designated spots…

If anyone could give me an idea of how to solve this problem it would be awesome!

I can do the timer and the touch to destroy, I’m just unsure of how to spawn an actor in a random designated location…

Would I make a function to spawn an ellipse in each designated spot then put those functions in a table to randomly choose one? So confused…

I don’t have much of the code written because I got stuck right in the beginning…

Here is all I have:

``````displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
ellipse{}
ellipseMode(CENTER)

end

function spawn()
fill(255, 0, 0, 255)
table.insert(ellipse,)
end

function draw()
background(0, 0, 0, 255)

end

``````
``````--in setup
spawnPos={vec2(100,100,vec2(200,200),vec2(300,300),vec(400,400)}

function Spawn()
local i=math.random(1,4)
--spawn at spawnPos[i]
end
``````

EDIT @stevon8ter - no mine isn’t smaller than yours…

@Crumble, you could store all the locations of the 4 spots in a table

``````ellipsLoc = {vec2(x1, y1), vec2(x2, y2), vec2(x3, y3), vec2(x4, y4)}
``````

then you create a random var

``````random = math.random(1,4)
``````

this chooses a random number between 1 and 4

then you ‘spawn’ the ellipse using something like…

``````table.insert(ellipses, ellipsLoc[random]
``````

and then you can draw them using the ellipses table…

Hope this helped

EDIT: darn it, @Ignatz is faster than me + his solution is probably smaller and quicker

``````displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
-- not ellipse{}, but ellipse = {}
elips = {}
ellipseMode(CENTER)
spawnPos={vec2(200,200),vec2(350,350),vec2(500,500),vec2(750,200)}

end

function spawn()
local random=math.random(1,4)
--shouldn't have the fill here
--fill(255, 0, 0, 255)
table.insert(elips,spawnPos[random])
end

function draw()
background(0, 0, 0, 255)
-- spawn an ellipse every frame? That's about 60 a second... wouldn't that be to much?
spawn()
fill(255, 0, 0, 255)
-- but this is for the drawing
for a,b in ipairs(elips) do
ellipse(elips[a].x, elips[a].y, 50)
end
end
``````

EDIT: I changed the elips table name, because it got confused with ellipse (the drawing command)

Hmm I am stuck again, getting an error on line 14. Where do I tie in the actual ellipse drawing and ellipse dimensions? Sorry for the newbie questions.

``````displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
ellipse{}
ellipseMode(CENTER)
spawnPos={vec2(200,200),vec2(350,350),vec2(500,500),vec2(750,200)}

end

function spawn()
local random=math.random(1,4)
fill(255, 0, 0, 255)
table.insert(ellipse,spawnPos[random])
end

function draw()
background(0, 0, 0, 255)
spawn()
end
``````

You guys are awesome! Thanks much!

Quick question… what is the difference between “in ipairs” and “in pairs”?

Also, I have the timer working for getting the ellipses to spawn every second or so, and destroy on touch. I am now stuck on how to get the ellipses to destroy automatically after 1 second. Any tips would be awesome!

Here is the code so far:

``````displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
-- not ellipse{}, but ellipse = {}
elips = {}
ellipseMode(CENTER)
spawnPos={vec2(200,200),vec2(350,350),vec2(500,500),vec2(750,200)}
timer=0
spawnFreq=1
score=0

end

function spawn()
local random=math.random(1,4)
--shouldn't have the fill here
--fill(255, 0, 0, 255)
table.insert(elips,spawnPos[random])
end

function draw()
background(0, 0, 0, 255)
-- spawn an ellipse every frame? That's about 60 a second... wouldn't that be to much?
fill(255, 0, 0, 255)
-- but this is for the drawing
for a,b in ipairs(elips) do
ellipse(elips[a].x, elips[a].y, 100)
end
timer=timer+DeltaTime
if timer>spawnFreq then
spawn()
timer=0
end
for a,b in ipairs(elips) do
if CurrentTouch.state == BEGAN then
if CurrentTouch.x>b.x-50 and CurrentTouch.x<b.x+50
and CurrentTouch.y>b.y-50 and CurrentTouch.y<b.y+50 then
table.remove(elips,a)
sound("Game Sounds One:Kick")
score=score+100
end
end
end
end

``````

@Crumble Here’s an example that shows the difference for pairs and ipairs.

``````
function setup()
tab1={a="aa",1,2,3,4,5,6,7,8,b="bb"}
print("pairs")
for a,b in pairs(tab1) do
print(a,b)
end
print("\
ipairs")
for a,b in ipairs(tab1) do
print(a,b)
end
end

``````