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?
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]
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]