Balloon Bust Error

Hello. I have an error in my game, “Balloon Bust” and I would like some help. One problem is that sometimes when you try to pop one balloon, all of the balloons pop. Another problem is when there’s one balloon left, and you try to pop it, an error shows up in the output section.
This is the code:

--# Main
-- Balloon Bust

-- Use this function to perform your initial setup
function setup()
    balloons = {balloon(5),balloon(7),balloon(3)}
    loaded = true

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 183, 255, 255)

    -- This sets the line thickness

    -- Do your drawing here
    for i,b in ipairs(balloons) do
        b.pos.y = b.pos.y + 2
    if CurrentTouch.state ~= ENDED then
        if loaded == true then
            loaded = false
            tween.delay(1,function() loaded = true end)
            local vt = vec2(CurrentTouch.x,CurrentTouch.y)
            for i = #balloons, 1, -1 do
                if vt:dist(balloons[i].pos) < balloons[i].size then
                    balloons[i] = nil

function touched(t)

function balloon(s,x,y)
    local colors = {color(31, 84, 45, 200),color(245, 0, 255, 200),color(255, 0, 133, 200),color(0, 255, 159, 200),color(0, 0, 0, 200)}
    return {size = s*30, colour = colors[math.random(1,#colors)], pos = vec2(x or math.random(0,WIDTH),y or 0)}

@Kolosso - when your code is so short, and the problem is probably very simple, it’s important you be able to debug it yourself, or you will never be able to write a program that does anything very exciting.

The first thing I would do is put a print statement just before you pop a balloon, and print the loop counter i, and the position of the balloon being popped.

I would run the program until either problem occurred, and see what the print statement gave me, and see if I can figure out the problem.

If you need to, put print statements in other places, or print out more details, until you have a good idea of what is actually happening.

After all, if we solve it for you, that is exactly what we would do.

But it’s better if you solve this one, to get practice in debugging. It should be a simple error.

@Ignatz, but @Kolosso is new

Everyone is new to programming and Codea when they start, even I was. If you don’t want to try and learn anything, then you might as well give up now. You can’t constantly keep asking for help. Well, I guess you could, but eventually no one is going to help. Anyone can do the same thing that we would do, put print statement at certain points in the code and see what the results are. That’s how you learn to debug your own code.

Ok. thanks everyone! I’ll try printing everything out.

@Kolosso You have to be careful where and how you put in print statements. If you put a print statement in draw or a function called from draw, you could end up with too many printouts that won’t help at all. You’ll learn as you try.

@TokOut - we will still help him if necessary, but it’s important that you learn to solve your own problems as much as possible.

@Kolosso - what dave says is right, but in this case, I don’t think it matters if you have lots of stuff printing out from draw, because what you are interested in, is only the last couple of messages which will be printed just above the error, when it happens.

I found out what the problem was! :smiley:
The problem was that the

balloons[i] = nil

wasn’t deleting the item, but replacing the item with nil!
Once I replaced that line with this:


, everything worked!

@Kolosso Good job. The next thing you need to do is get rid of CurrentTouch and start to use the touched function. You’ll find it easier to use and give you more control over screen touches.

I purposely did’nt use the touch function because the touch function isn’t called when you don’t move your finger.

The touched function is called when you touch the screen, when you move your finger on the screen, and when you lift your finger from the screen.

Yes, BEGAN, MOVING, and ENDED. There isn’t any HOLDING or anything, and the player shoots by touching the screen and their reloaded. I don’t want them having to move their finger around while they’re waiting to be reloaded.

Edit: Sorry if that looked like yelling or something

There is holding. It’s the time between when BEGAN happens and when ENDED happens.

@Kolosso - using the touched function gives you much more control, and while it may seem like overkill for what you are doing now, t’s well worth learning.

the way you handle HOLDING is like this

function touched(t)
    if t.state==BEGAN then holding=true
    elseif t.state==ENDED then holding=false

function draw()
    --other code
    if holding then

@ignatz Oh. I haven’t thought of doing that. I’ll write that right now.

@Ignatz @dave1707 Okay. So now I’m gonna make the balloons spawn at random times. Do you know what’s the best way to make a timer that runs a function? Should I use


or should I make a whole new variable that counts down, and then check is it’s 0 and run the function?

If you don’t care how accurate it is, add 1 to a variable in draw and if it’s some value, spawn your balloon and zero the variable. A count of 60 will be about 1 second.