Looking for help, simple problem.

I’m very new to Codea and I am trying to create a program that spawns multiple physical objects(circles) and an ellipse to go with each one. I want the objects to spawn every time there is a touch. I have been able to spawn ONE object exactly where I touch, but of course when I tap again it just disappears and spawns a new one.

I feel like I’m supposed to be using a table to manage the objects but I’m really not sure what to do. I would really appreciate the help!

Hope this can help you

-- physics

-- Use this function to perform your initial setup
function setup()
    balls = {}
    
    --makes sure you only generate one ball per touch
    lastTouchState = nil
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)
    
    fill(255, 0, 0, 255)
    
    --generate the balls
    if CurrentTouch.state == BEGAN and
    lastTouchState ~= BEGAN then
        table.insert(balls, physics.body(CIRCLE, 25))
        balls[table.maxn(balls)].position = vec2(CurrentTouch.x, CurrentTouch.y)
    end
    
    lastTouchState = CurrentTouch.state
    
    --Draw the balls
    for i,v in ipairs(balls) do
        ellipse(v.position.x, v.position.y, 50)
    end
end

I usualy use touched() but in my opinion, using CurrentTouch is faster to code than touched()

Hi!
Check this for ideas: http://twolivesleft.com/Codea/Talk/discussion/1321/kinda-like-spawn/p1
:slight_smile:

Out of curiosity, why not use the touched() event for touches? I know it’s not in the help files, but some of the examples use this, and it seems cleaner than remembering the touch state.

Thy whit this , only need to change the character to Circles (fhysics.body)

http://pastebin.com/GHFyKCtz