How do I place a newText on a physics body?

I’m trying to implement a bunch of newCircle objects that move about randomly. Each circle will have a random number label on them.

How would I go about placing a newText object on the ball while it is moving, so that they are actually one combined entity?

You can either put them in a group (ball and text) at the group origin, and then create the physics body on the group rather than on the circle, or you can use an enterFrame event like this:
 

local ball = --create ball physics.addBody... --add body to ball local text = --create a text object function ball:enterFrame() text.x, text.y = self.x, self.y; end Runtime:addEventListener("enterFrame", ball);

Thank you!  That really helped.

I had an array of the balls, so I did this:

Runtime:addEventListener( "enterFrame", moveText) moveText = function(event)   for i = 1, num\_balls do      text[i].x = balls[i].x      text[i].y = balls[i].y   end end

Nice, glad it helped! (; 
 

Hi @dislam,

Another option, using just physics, would be to weld the text object to the ball using a weld joint. That would be my approach, but then, I’m usually looking for physics-only-based ways to solve physics-based scenarios. :stuck_out_tongue:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#weld

Brent

Thanks for the tips! Now, I’m trying to figure out why the collisions between the balls are a bit early. For example if they touch each other on the top or bottom, the gap between them is tiny, but on the sides or corners a bigger gap still causes collision?

Nevermind, figured it out… there is a “radius” property in addBody which sets the bounding circle limits.  Doh!

You can either put them in a group (ball and text) at the group origin, and then create the physics body on the group rather than on the circle, or you can use an enterFrame event like this:
 

local ball = --create ball physics.addBody... --add body to ball local text = --create a text object function ball:enterFrame() text.x, text.y = self.x, self.y; end Runtime:addEventListener("enterFrame", ball);

Thank you!  That really helped.

I had an array of the balls, so I did this:

Runtime:addEventListener( "enterFrame", moveText) moveText = function(event)   for i = 1, num\_balls do      text[i].x = balls[i].x      text[i].y = balls[i].y   end end

Nice, glad it helped! (; 
 

Hi @dislam,

Another option, using just physics, would be to weld the text object to the ball using a weld joint. That would be my approach, but then, I’m usually looking for physics-only-based ways to solve physics-based scenarios. :stuck_out_tongue:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#weld

Brent

Thanks for the tips! Now, I’m trying to figure out why the collisions between the balls are a bit early. For example if they touch each other on the top or bottom, the gap between them is tiny, but on the sides or corners a bigger gap still causes collision?

Nevermind, figured it out… there is a “radius” property in addBody which sets the bounding circle limits.  Doh!