Stop a dynamic physics object directly by tapping the screen

I have a dynamic physics object in a gravityless (not sure if that’s a word) environment in my project.

Here’s my code:

[lua]

local mask = display.newImageRect(“transparentmask.png”, 320, 568)

local ball = display.newImage(“ball.png”)

local check = function(event)

    boostxf = event.xStart - event.x

    boostyf = event.yStart - event.y

    

    boostx = boostxf/-200

    boosty = boostyf/-200

    

    ball:applyLinearImpulse( boostx, boosty, ball.x, ball.y )

    

end

mask:addEventListener(“touch”, check)

[/lua]

I had made a function that applies linear impulses on the ball based on swipe movements. It works, but I would also like to make the ball stop completely when the user taps the screen, but I don’t know how to do this.

Any help or tips are appreciated :slight_smile:

It looks to me like you’ll first need to amend your touch listener to detect a swipe gesture, and a “tap”. Try modifying your check function to something like this:

function check(event) local phase = event.phase if phase == "began" then elseif phase == "moved" then elseif phase == "cancelled" or phase == "ended" then end end

Inside your touch handler, you’ll need to check whether the “moved” phase was initiated. If it wasn’t, then you can “assume” the touch was a tap (or long gesture). There are some good examples of how to do this in the sample code files provided by Corona. Have a look under CoronaSDK > SampleCode > Interface for some examples.

If you can store a reference to your ball somewhere, you can then “freeze” its movement by setting the x/y component of the linear velocity on the ball to 0, 0.

So…

ball:setLinearVelocity(0, 0)

You can also change it’s body type to static and make it sleep:

ball.isAwake = false

ball.bodyType = “static”

Hopefully that helps.

Thanks!  It did help a lot!

It looks to me like you’ll first need to amend your touch listener to detect a swipe gesture, and a “tap”. Try modifying your check function to something like this:

function check(event) local phase = event.phase if phase == "began" then elseif phase == "moved" then elseif phase == "cancelled" or phase == "ended" then end end

Inside your touch handler, you’ll need to check whether the “moved” phase was initiated. If it wasn’t, then you can “assume” the touch was a tap (or long gesture). There are some good examples of how to do this in the sample code files provided by Corona. Have a look under CoronaSDK > SampleCode > Interface for some examples.

If you can store a reference to your ball somewhere, you can then “freeze” its movement by setting the x/y component of the linear velocity on the ball to 0, 0.

So…

ball:setLinearVelocity(0, 0)

You can also change it’s body type to static and make it sleep:

ball.isAwake = false

ball.bodyType = “static”

Hopefully that helps.

Thanks!  It did help a lot!

can any one help me with object trajectory not stopped http://forums.coronalabs.com/topic/43378-object-not-stoping-in-my-trajectory-function/?p=225880

Hi @twins.alfian,

With all due respect, please just wait for responses on the thread you created, instead of linking it up within 3 or more other threads. The forums are quite active, and I see you already received a response in your original thread.

Thanks for your cooperation, and best of luck with your project.

Brent

can any one help me with object trajectory not stopped http://forums.coronalabs.com/topic/43378-object-not-stoping-in-my-trajectory-function/?p=225880

Hi @twins.alfian,

With all due respect, please just wait for responses on the thread you created, instead of linking it up within 3 or more other threads. The forums are quite active, and I see you already received a response in your original thread.

Thanks for your cooperation, and best of luck with your project.

Brent