Spawning multiples of same actor.

Hello all!

First post on the forums and trying to learn how to use Codea, these forums are a wonderful resource for learning!

I am very new to Codea and programming in general, so is may be a very basic question…

On to my question, I am messing around with a little “game” where I have a background and I have circles spawning every half second in a random X location above the screen and dropping from the top of the screen to off the bottom of the screen. My problem is that I can only get the first circle to spawn, I can’t seem to figure out how to make Codea keep spawning new circles while keeping the previous circle as well. Here is what I have:

-- Testtttt
function setup()
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    AsteroidWidth=100
    AsteroidHeight=100
    EllipseStart=818
    EllipseRandomX=math.random(50,WIDTH-50)
    EllipseSpeed=-15
    LaunchEllipse()
    timer=0
    asteroidFrequency=2
end
function LaunchEllipse()
    fill(255, 238, 0, 255)
        ellipse(EllipseRandomX,EllipseStart,AsteroidWidth,AsteroidHeight)
        EllipseStart=EllipseStart+EllipseSpeed
end
function draw()
    sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    timer=timer+DeltaTime
    if timer>1/asteroidFrequency then
    LaunchEllipse()
end
    end

If anyone could give me any pointers to how I can get Codea to keep spawning the circles, I would greatly appreciate it!

Thanks in advance!

@Crumble - welcome!

I think you are assuming that when you draw an ellipse, it will keep drawing itself.

Codea is not like other software, though. It is an animation program. Think of a big pile of sheets of paper, each one of them a frame in the animation. Each sheet has to be drawn on separately. So if you have a circle moving down the screen, then you have to draw it over and over again (in the draw function), moving it slightly down each time.

You need a table to hold your circles in, and a loop in your draw function that draws all the circles in it, adjusts their position, and deletes any that fall off the screen.

I suggest some tutorials and/or reading other people’s code. I can offer you beginner ebooks and posts here

http://coolcodea.wordpress.com/2013/06/19/index-of-posts/

and if you search for starter games in the search box above, you should find plenty of examples.

Here’s your code modified to work.


-- Testtttt

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    tab={}  -- table for asteroids
    AsteroidWidth=100
    AsteroidHeight=100
    EllipseStart=818
    EllipseRandomX=math.random(50,WIDTH-50)
    EllipseSpeed=-15
    LaunchEllipse()
    timer=0
    asteroidFrequency=2
end

function LaunchEllipse()
    -- insert asteroid x,y values in table
    table.insert(tab,vec2(math.random(50,WIDTH-50),EllipseStart))
end

function draw()
    sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    
    -- draw asteroids
    fill(255, 238, 0, 255)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,AsteroidWidth,AsteroidHeight)
        b.y=b.y+EllipseSpeed
    end
    
    -- remove asteroids that go off bottom of screen
    for a,b in pairs(tab) do
        if b.y<0 then
            table.remove(tab,a)
        end
    end
    
    -- check timer to launch next asteroid
    timer=timer+DeltaTime
    if timer>asteroidFrequency then
        LaunchEllipse()
        timer=0
    end
end

@Crumble I added comments to my code above to help you understand what I did.

assuming an actor is an instance of a class (or anything really), put them in a table

You guys are awesome.

Thanks much Dave, that is exactly what I was trying to do, now I have to wrap my head around it.

Thanks again guys.