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={}
ball=readImage("Space Art:Asteroid Small")
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={}
ball=readImage("Space Art:Asteroid Small")
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
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.