Tables - mine has three legs

Hello everyone - thanks for the help in my last post.. it got me in the right direction but now I'm stumped with a new problem.

I'm dropping a smaller ball into a bucket and upon doing so the ball is deleted and is replaced with a smaller ball at those coordinates. I'm using a table to track the creation of smaller balls.

I know my code is working fine up to the "print(#allballs)" line of code since I can see its adding to the index each time a small ball is dropped into the bucket. The code is also successfuly deleting the balls and replacing them with a new images of a smaller ball at the last coordinates of the larger ball. However, I'm stuck as to how I go about making the newly created smaller ball image a physical object.

I get a runtime error that at my physics.addbody line tell me that I'm attempting to call local 'ball' (a table value)

allballs[#allballs + 1] = display.newImage ("LargeBall.png")
local ball = allballs[#allballs]
ball.x = onesmallball.x
ball.y = onesmallball.y
ball.rotation = math.random (1, 360)
onesmallball:removeSelf()
print( #allballs )

physics.addBody( ball { density=0.4, friction=0.5, bounce=0.3 } )

Any ideas? My weak and fragile newbie hunch tells me that the device doesnt know which of the small balls to make physical... but then how does it know which of the small balls to turn into an image?

Thanks for your help!

grrr cant seem to edit my post... first line is supposed to say "I'm dropping a larger ball into a bucket and upon doing so the ball is deleted and is replaced with a smaller ball at those coordinates..."

Hi.

Given the code you posted.

1
2
3
4
5
6
7
8
9
allballs[#allballs + 1] = display.newImage ("LargeBall.png")
local ball = allballs[1] -- Does this work?
ball.x = onesmallball.x
ball.y = onesmallball.y
ball.rotation = math.random (1, 360)
onesmallball:removeSelf()
print( #allballs )
 
physics.addBody( ball, { density=0.4, friction=0.5, bounce=0.3 } ) --(was missing a comma after ball

I have an earlier line of code:   local allballs = {} ... Is that what you are referring to?

No sorry. I figure your attempting to index a table value that isn't there (yet) or has not being assigned at the time of execution.

actually - I just realized that you made a comment in your earlier post regarding a missing comma - that was it!

Thank you!!

p.s. how do you put code in that format?

No probs :)

You do this

"<"code">"

--code here

"<"/code">"

(without the quotes ;) )

views:1336 update:2011/10/11 8:57:46
corona forums © 2003-2011