Manipulating multiple physical bodies

Hello everyone!

I'm very new to corona and I would love some help. I hope my question makes sense!....

I'm working with a bucket and some balls. Every time a button is pressed, a ball falls from the top of the screen into the bucket. Multiple balls can fill the bucket.

My question is, what is the best strategy for handling the balls on an individual basis. For example, if I wanted to drag one of the fallen balls out of the bucket, or if one of the balls falls out of the bucket something should happen.

Currently, I am unable to manipulate balls individually because they are all "local ball"

Hopefully someone can point me in the right direction!

Thanks!

store them to a table and then you can reference them all you want

Thanks for the quick response! I had a feeling that tables were the direction I needed to be heading in, but I didn't want to go down that (daunting?) road needlessly.

On that note, anyone know of a good intro/tutorial on tables?

Thanks! =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--if you don't need to insert more balls at a later point, you can use this method of storing them.
local balls = {} --creates empty array.
 
balls[1] = display.newCircle( 100, 100, 30 )
balls[2] = display.newCircle( 100, 100, 30 )
balls[3] = display.newCircle( 100, 100, 30 )
balls[4] = display.newCircle( 100, 100, 30 )
 
--4 balls now in the array, now to reference them.
 
for i = 1, #balls do --starts at 1 and goes through all the values in your array. #balls = 4.
        
        --reference using i, means every ball in array is effected as you're not using direct reference such as balls[1] or balls[2]
        -- you're using a value that incrememnts every time the loop goes round.
        
        --makes every ball a different colour.
        balls[i]:setFillColor( i*55, 255-i*55, 220 )
        
        balls[i].x = 160
        balls[i].y = 100 * i
 
        print("i is now: " .. i)
 
end
 
--so in your touch listner for moving balls you would use ball[i] instead of ball. 
 
--look at the drag body sample code, in the sample code folder.
views:1318 update:2011/10/11 8:57:46
corona forums © 2003-2011