Pause 'applyForce' after drag event ended.

Hi there,

For starters I am new to Corona (and programming in general) . I am not getting my head around something (probably simple), I try to do the following. Coming from the Corona example script of the pool table, I would like to use the “applyForce” on the cue ball in a similar matter, BUT I would like to install a pause sequence before the ball fires (as I want to fire up some animation before the shot is fired) So pause after drag ends and then after pause use “applyForce”. This should be done by something like timer.performWithDelay… (I was thinking)
I have tried this:

if event.phase == ended  
  
-- some other code handling animation here  
  
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  
timer.performWithDelay (5000, cueShot, 1)   
end  
  
Runtime:addEventListener( "cueShot", applyForce )  

but I am doing it basically wrong (applyForce is not a function?)
trying to wrap it in a function like so under

if event.phase == ended  
  
-- some other code handling animation here  
  
-- and then  
function delayShot (self, event)   
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  
timer.performWithDelay (5000, delayShot, 1)   
end  

does only delay the animation of the rotation, not the actual shot.
Can anybody tell me what I am doing wrong? Thnx a lot

[import]uid: 193590 topic_id: 34104 reply_id: 334104[/import]

I don’t really understand your code o_O.

If you are fine with having all objects pause(which would make sense in the pool scenario) you could just pause the physics engine, play your animation and then start the physics engine again. Below is some pseudo-code for how I would handle it.

[lua]local function myCollisionEvent(event)

if event.object1 == “cueball” or event.object2 == “cueball” then
local function restartPhysics()
physics.start()
end
physics.pause()

timer.performWithDelay(5000, restartPhysics)

–do my cool animation

end

end[/lua]
Depending on how your animation works you could use some sort of animation end event(I believe the sprite library has that event) instead of the timer. That’s what I would do except I don’t know the sprite api well enough off the top of my head :x [import]uid: 147305 topic_id: 34104 reply_id: 135610[/import]

if event.phase == ended  
   
-- some other code handling animation here  
   
-- and then  
function delayShot (self, event)   
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  
timer.performWithDelay (5000, delayShot, 1)   
end  

The reason why this isn’t working is for the following reasons:

  1. You are creating a timer to re-run the exact function, even if you made it run only once, it will run infinitely because once it re-runs it re-creates the timer again, going on forever. You want to put the timer in the animation area so that the timer starts ticking when the the animation starts playing.
  2. That is if it would run the function correctly though. Another reason why it wouldn’t work is because by using the timer it won’t know who ‘t’ is because you did not click for it. So once the timer runs it would give you an error most likely I think. Not 100% on this one but just a guess.
    Hope this all makes sense. Anyway let me give you an easier way:
if event.phase == ended then  
  
cueBallAnimation:play()  
CueAnimationTimer = timer.performWithDelay( 5000, delayShot, 1 )  
  
focusBall = event.target  
focusLocX = event.x  
focusLocY = event.y  
  
--you should but this function outside this function  
function delayShot()   
focusBall:applyForce( (focusBall.x - focusLocX), (focusBall.y - focusLocY), focusBall.x, focusBall.y )  
end  
  
end  

Something like that off the top of my head. Hope it helps [import]uid: 77199 topic_id: 34104 reply_id: 135652[/import]

@budershank @hatethinkingofnames thnx! Need to test both. Awesome that you’ve reached out to help. Will report here on where this Will take me. Thnx again! [import]uid: 193590 topic_id: 34104 reply_id: 135924[/import]

I don’t really understand your code o_O.

If you are fine with having all objects pause(which would make sense in the pool scenario) you could just pause the physics engine, play your animation and then start the physics engine again. Below is some pseudo-code for how I would handle it.

[lua]local function myCollisionEvent(event)

if event.object1 == “cueball” or event.object2 == “cueball” then
local function restartPhysics()
physics.start()
end
physics.pause()

timer.performWithDelay(5000, restartPhysics)

–do my cool animation

end

end[/lua]
Depending on how your animation works you could use some sort of animation end event(I believe the sprite library has that event) instead of the timer. That’s what I would do except I don’t know the sprite api well enough off the top of my head :x [import]uid: 147305 topic_id: 34104 reply_id: 135610[/import]

if event.phase == ended  
   
-- some other code handling animation here  
   
-- and then  
function delayShot (self, event)   
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )  
timer.performWithDelay (5000, delayShot, 1)   
end  

The reason why this isn’t working is for the following reasons:

  1. You are creating a timer to re-run the exact function, even if you made it run only once, it will run infinitely because once it re-runs it re-creates the timer again, going on forever. You want to put the timer in the animation area so that the timer starts ticking when the the animation starts playing.
  2. That is if it would run the function correctly though. Another reason why it wouldn’t work is because by using the timer it won’t know who ‘t’ is because you did not click for it. So once the timer runs it would give you an error most likely I think. Not 100% on this one but just a guess.
    Hope this all makes sense. Anyway let me give you an easier way:
if event.phase == ended then  
  
cueBallAnimation:play()  
CueAnimationTimer = timer.performWithDelay( 5000, delayShot, 1 )  
  
focusBall = event.target  
focusLocX = event.x  
focusLocY = event.y  
  
--you should but this function outside this function  
function delayShot()   
focusBall:applyForce( (focusBall.x - focusLocX), (focusBall.y - focusLocY), focusBall.x, focusBall.y )  
end  
  
end  

Something like that off the top of my head. Hope it helps [import]uid: 77199 topic_id: 34104 reply_id: 135652[/import]

@budershank @hatethinkingofnames thnx! Need to test both. Awesome that you’ve reached out to help. Will report here on where this Will take me. Thnx again! [import]uid: 193590 topic_id: 34104 reply_id: 135924[/import]

Hi Budershank and hatethinkingofnames.

Just got back from a massive flew attack om me and my family. So much for the christmas fun and food (no thanks)

So a very late reply to your help. For which I want to thank you sincerely.

Stopping the physics engine works to get time to do the animation (@budershank), it’s a logical solution. So this solves the problem. However I am not sure how this will work out with some waypoints moving targets on another part of the screen… (they need to keep animating, they may loose their physic qualities during the pause)

What I will do next is take a deep dive into the sprite api and test my waypoints (not in there yet)
tthnx so far, great to get good replies when you’re a noob and stuck with something!! awesome
[import]uid: 193590 topic_id: 34104 reply_id: 137702[/import]

Hi Budershank and hatethinkingofnames.

Just got back from a massive flew attack om me and my family. So much for the christmas fun and food (no thanks)

So a very late reply to your help. For which I want to thank you sincerely.

Stopping the physics engine works to get time to do the animation (@budershank), it’s a logical solution. So this solves the problem. However I am not sure how this will work out with some waypoints moving targets on another part of the screen… (they need to keep animating, they may loose their physic qualities during the pause)

What I will do next is take a deep dive into the sprite api and test my waypoints (not in there yet)
tthnx so far, great to get good replies when you’re a noob and stuck with something!! awesome
[import]uid: 193590 topic_id: 34104 reply_id: 137702[/import]