Working with multiple copies of same object?

Hello,
Can anyone recommend a tutorial on how to work with multiple physics objects that are created from the same graphic. For example, I have a new copy of a bubble graphic appear every time a button is pressed. After a few presses you have a few bubbles launched onto the screen (1 per press). Okay… so I want to monitor the velocities of each bubble so that if it goes negative (i.e., the bubble starts falling down instead of floating up) then that bubble is erased. The code for creating the bubbles is shown below. Forgive my ignorance but how do I refer to a specific bubble when all have the same name of “bubble”? Thanks so much!

[code]
– ####### BUTTON PRESS HANDLER #######

local mainButtonHandler = function( event )
if event.phase == “press” then
local bubble = display.newImage(“images/bubble007.png”)
bubble.myName=“bubble”
localGroup:insert(bubble)

– Determine where to start the bubbles from
if nozzleAngle>90 then
bubble.x=nozzle.contentBounds.xMin
bubble.y=nozzle.contentBounds.yMin
else
bubble.x=nozzle.contentBounds.xMax
bubble.y=nozzle.contentBounds.yMin
end

– Make bubbles a physics body
physics.addBody( bubble, “dynamic”, {density = 1.5, friction = 0.3, bounce = 0, radius = 6})

[code]
[import]uid: 16901 topic_id: 7237 reply_id: 307237[/import]

Okay… I think I understand now that I should be indexing my object like bubble[n]…
thanks! [import]uid: 16901 topic_id: 7237 reply_id: 25449[/import]

I can’t put any code down right now, but you should write a function which creates the bubble and all it’s handling code, then return the bubble from the function. It can then be added to a list but will already be in the stage group, so you don’t have to.

M [import]uid: 8271 topic_id: 7237 reply_id: 25476[/import]

Ok, just to follow up my last post, here’s a little code which adds a bubble image to the screen and makes it touch sensitive…

m

[lua]function createBubble( dispgroup, id, x, y )
– create bubble image in bubbles display group
local bubble = display.newImage( dispgroup, “bubble.png” )
– name it
bubble.name = id
– position it
bubble.x, bubble.y = x, y
– add touch function
function bubble:touch( event )
print(“I feel touched”, event.phase)
– indicate the bubble handled it’s touch,
– otherwise touch event propagates to next object below
return true
end
– return the bubble object, though this can be ignored
return bubble
end

– create display group for bubbles only
local bubbles = display.newGroup()
– create 3 bubbles
for i=1, 3 do
– create new bubble at unique location in bubbles display group
createBubble( i, bubbles, i*50, 250 )
end[/lua]

Ps: Sorry about the formatting. [import]uid: 8271 topic_id: 7237 reply_id: 25485[/import]

Thanks so much for taking the time to put some code up… just to make sure… I can then “inspect” each bubble by cycling through the bubbles group like
for i=1,bubbles.numChildren do
etc.

correct? [import]uid: 16901 topic_id: 7237 reply_id: 25579[/import]

I got it working… thanks!!! [import]uid: 16901 topic_id: 7237 reply_id: 25597[/import]

Nice one.

Yes, cycling through display groups requires using the numChildren property, whereas cycling through tables requires #.

I prefer the former because a display group is otherwise just like a regular table and sometimes removing display objects group a display group can make you think you’ve removed every reference, when there might still be one lurking in a hard to find table index.

m [import]uid: 8271 topic_id: 7237 reply_id: 25633[/import]