# Little problems

Hi all!

I’m messing with a little program, trying to get pairs of ellipses to spawn every second randomly at 2 of 4 designated locations.

I have 3 problems though…

First, the ellipses stop spawning after spawning 1-3 times.

Second, for some reason some of the ellipses increase in speed as they get to the bottom of the screen, they should be moving at a constant velocity.

Third, I can’t figure out how to get the ellipses to not spawn 2 ellipses at the same location…

Any tips on how to fix any of these problems would be greatly appreciated!

Here is what the code looks like:

``````
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)

-- Use this function to perform your initial setup
function setup()
balls={}
spawnLoc={vec2(WIDTH/2.7,1152),vec2(WIDTH/7.8,1152),
vec2(WIDTH/1.6,1152),vec2(WIDTH/1.16,1152)}
ballSpeed=-15
counter=0
ballFreq=60
end

function ballSpawn()
local random = math.random(1,4)
table.insert(balls,spawnLoc[random])
end
-- This function gets called once every frame
function draw()

background(0,0,0,255)

counter=counter+1

fill(255, 0, 0, 255)

for a,b in ipairs(balls) do
ellipse(b.x,b.y,128,128)
b.y=b.y+ballSpeed
end

if counter%ballFreq==0 then
ballSpawn()
ballSpawn()
end

end

``````

@Crumble - Try this, no time to explain, will do so in a little while

``````
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)

-- Use this function to perform your initial setup
function setup()
balls={}
spawnLoc={vec2(WIDTH/2.7,1152),vec2(WIDTH/7.8,1152),
vec2(WIDTH/1.6,1152),vec2(WIDTH/1.16,1152)}
ballSpeed=-15*60
counter=0
ballFreq=60
end

function ballSpawn(n)
local t={1,2,3,4}
for i=1,n do
local random = math.random(1,#t)
table.insert(balls,vec2(spawnLoc[t[random]].x,spawnLoc[t[random]].y))
table.remove(t,random)
end
end
-- This function gets called once every frame
function draw()

background(0,0,0,255)

counter=counter+1

fill(255, 0, 0, 255)

for a,b in ipairs(balls) do
ellipse(b.x,b.y,128,128)
b.y=b.y+ballSpeed*DeltaTime
if b.y<0 then b=nil end
end

if counter%ballFreq==0 then
ballSpawn(2)
end

end

``````

ok, I fixed the balls so they don’t start in the same place

the reason the balls speeded up and also why they stopped appearing is explained in this post

http://coolcodea.wordpress.com/2013/06/14/84-tables-and-pointers/

While that post is about tables, the same applies to vec2

So

``````a=vec2(1,1)
b=a
c=vec2(a.x,a.y)
b.x=3
print(a) -- (3,1)
print(b) -- (3,1)
print(c) -- (1,1)
``````

In this example, b is not given a copy of a, but is given the address of a, so it is pointing at the same vec2. This means any change to b also affects a.

On the other hand, c is created by coping the individual x and y values of a, and because these are just numbers, Codea does make separate copies of them.

Awesome, that works perfect. Now to wrap my head around what is going on in the ballSpawn function.

Thanks for the help!