How to detect if all bodies have stopped moving

I’m working on a billiards-like game, and I’d like to disable the player from making a move until after all ball movements have been resolved. Is there a built-in event for this? Or do I have to create an enterFrame listener and check the physics bodies one by one?

Thanks. [import]uid: 7026 topic_id: 6058 reply_id: 306058[/import]

Create an enterFrame listener that checks all physics bodies? [import]uid: 30185 topic_id: 6058 reply_id: 20763[/import]

Could this be explained in greater detail? How would you create an enterFrame listener that checks all physics bodies?

I would also like to be able to check if all bodies were stationary before allowing the player to continue. [import]uid: 31694 topic_id: 6058 reply_id: 20878[/import]

You can check the linear velocity of the object, if the magnitud is less than a threshold that you defined then is consider that the object is stopped

local vx,vy = object:getLinearVelocity() local m = math.sqrt((vx\*vx)+(vy\*vy)) if m<threshold then> —Consider this object as stopped<br>end<br>
[import]uid: 9975 topic_id: 6058 reply_id: 20895[/import]

Get the linear velocity of all the objects. Then create an If function inside an enterFrame and check if all linear velocities + each other = 0, if it’s true, set some variable called isActive = true, if it’s not true set isActive = false, which makes the player unable to move the ball [import]uid: 30185 topic_id: 6058 reply_id: 20927[/import]

I’m having trouble with Lua. Below is my code:

function newBean()  
 newBean = display.newImage( "image.png" );  
 newBean.x = display.contentWidth\*0.5  
 newBean.y = -100  
 newBean.myName = "bean1"  
 physics.addBody( newBean, { density=0.9, friction=0.3, bounce=0} )  
  
end  
  
function moveTest(event)  
 local vx,vy = bean1:getLinearVelocity()  
 print (vx,vy)  
end  
Runtime:addEventListener( "enterFrame", moveTest )  

When I try to refer back to “bean1” in the second function I get a runtime error that this is a nil value. How can I refer back to objects created in other functions in order to report on their status later on? [import]uid: 31694 topic_id: 6058 reply_id: 22329[/import]

You’re referring to bean1 as if it were a variable (which it isn’t, it’s just a property value on your newBean object).

Try declaring a variable (local bean1) at the top of your code then use that instead. [import]uid: 8353 topic_id: 6058 reply_id: 22556[/import]

Do this:

function newBean()  
 bean= display.newImage( "image.png" );  
 bean.x = display.contentWidth\*0.5  
 bean.y = -100  
 bean.myName = "bean1"  
 physics.addBody( bean, { density=0.9, friction=0.3, bounce=0} )  
 return bean  
end  
  
local bean1 = newBean()  

[import]uid: 9975 topic_id: 6058 reply_id: 22724[/import]