[Resolved] Unique identifiers for an array

2 replies [Last post]
hatethinkingofnames
User offline. Last seen 16 weeks 6 days ago. Offline
Joined: 19 Jul 2011

I think that's the right title. What I'm trying to do is use a function that spawns stuff on a timer and that each thing it spawns is set like [1], [2], [3] etc so that later I can make another function that controls all of the items spawned. Here's a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
balloon = {}
local i = 1
 
local function spawnStuff()
                 if spawnTrue == true then
                        local randomPos = math.random ( 0, 320 )
                        balloon[i] = display.newImage( "something.png" )
                        balloon[i].x = randomPos
                        balloon[i].y = 230
                        balloon[i].myName = "balloon"
                        physics.addBody(balloon[i] , "dynamic", { radius = 17 })
                        i = i + 1
                 end
 
end
 
 
timer.performWithDelay( 500, spawnStuff, 500 )

The way I see this function is every 0.5 seconds it should create a new balloon, and that balloon should be indexed as balloon[i] where i = i + 1 after every function. So shouldn't every balloon that gets spawned be indexed balloon[1], balloon[2] etc ? But when I try to do something with balloon[2] it doesnt work, I get an error because it doesnt exist from the start. I did balloon[1].myName = "balloon" that didnt help. How would I make it work? What I'm trying to do is a more advanced version of the 'create your own game in 8 minutes' mostly for practice purposes. I have this :
1
2
3
4
5
6
local function moveStuff(event) 
        local ball = event.target
        ball:applyLinearImpulse( 0, -0.2, event.x, event.y )
end
 
balloon:addEventListener("touch", moveStuff )

Ive tried a lot of things nothing seems to work. How do I give each balloon spawned a unique identifier that I can later call, and how do I make it so for an eventListener I only have to write one line of code for all the balloons to work. hope im making sense, thanks in advance for any help

Replies

hatethinkingofnames
User offline. Last seen 16 weeks 6 days ago. Offline
Joined: 19 Jul 2011

Managed to solve my own problem and learned something new. Didn't know I could addEventListeners in functions, damn that is so useful and makes things so easy. Corona and lua are so freaking epic.

All it was was adding the eventlistener in the function for balloon[i]

peach pellen
User offline. Last seen 1 year 32 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

Glad to see you got this resolved, thank you for updating :)

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.