transition.resume does work on applicationResume

I’ve got the following code in my application:

[lua]

Runtime:addEventListener(“system”,function (event)

  if event.type==“applicationSuspend” then

    transition.pause()

  end

  if event.type=="applicationResume"then

     transition.resume()

  end

end)

[/lua]

I use it to stop and resume a scrolling background effect. I’ve found that if I don’t do that, the animation jumps when the application resumes. However I’ve noticed that the transition only resumes every second time the code is called. So if I open the application after suspending it nothing happens. If I do it again it works. Any ideas on how to work around this?

Hi @tap32,

Out of curiosity, can you move the function you’ve wrapped inside this listener up and outside and then call it via a short timer of like 100 milliseconds? This kind of thing may need to triggered after a short timer, but I haven’t tested as such before.

Thanks,

Brent

Actually that causes the code to never work! That’s a massive surprise to me! 

I created this quick test case for it. I’ve not tried this on the device yet, just in the simulator, so the behaviour may be different.

[lua]

local c=display.newCircle(display.contentCenterX,display.contentCenterY,20)

function move()

  transition.to(c,{x=c.x<display.contentCenterX and display.contentWidth or 0,onComplete=move})

end

move()

Runtime:addEventListener(“system”,function (event)

  if event.type==“applicationSuspend” then

    transition.pause()

  end

  if event.type=="applicationResume"then

    timer.performWithDelay(100,transition.resume)

  end

end)

[/lua]

Perhaps you should consider either saving a reference to the transition:

local myTransition = transition.to(…)

Then trying to resume based on passing that reference to transition.resume()… Or consider tagging your transitions and using tags to resume.  However, I wonder if this will work on Android at all since Android dumps the textures on suspend and we have to reconstruct them on resume.  This may be enough to potentially break the transitions.  You might also want to give a little more time to your timer to allow Android to get the display rebuilt.

Rob

Thanks Rob, in playing around with tagging I came across the solution that works for the simulator: my original test case was buggy as timer.performWithDelay passes an event object into the transition.resume function which must have confused the matter. A working test case looks like:

[lua]

local c=display.newCircle(display.contentCenterX,display.contentCenterY,20)

function move()

  transition.to(c,{x=c.x<display.contentCenterX and display.contentWidth or 0,onComplete=move})

end

move()

Runtime:addEventListener(“system”,function (event)

  if event.type==“applicationSuspend” then

    transition.pause()

  end

  if event.type=="applicationResume"then

    timer.performWithDelay(1,function() transition.resume() end)

  end

end)

[/lua]

Thanks for the Android tip, will have to play around with it once I get my hands on a device. 

Hi @tap32,

Out of curiosity, can you move the function you’ve wrapped inside this listener up and outside and then call it via a short timer of like 100 milliseconds? This kind of thing may need to triggered after a short timer, but I haven’t tested as such before.

Thanks,

Brent

Actually that causes the code to never work! That’s a massive surprise to me! 

I created this quick test case for it. I’ve not tried this on the device yet, just in the simulator, so the behaviour may be different.

[lua]

local c=display.newCircle(display.contentCenterX,display.contentCenterY,20)

function move()

  transition.to(c,{x=c.x<display.contentCenterX and display.contentWidth or 0,onComplete=move})

end

move()

Runtime:addEventListener(“system”,function (event)

  if event.type==“applicationSuspend” then

    transition.pause()

  end

  if event.type=="applicationResume"then

    timer.performWithDelay(100,transition.resume)

  end

end)

[/lua]

Perhaps you should consider either saving a reference to the transition:

local myTransition = transition.to(…)

Then trying to resume based on passing that reference to transition.resume()… Or consider tagging your transitions and using tags to resume.  However, I wonder if this will work on Android at all since Android dumps the textures on suspend and we have to reconstruct them on resume.  This may be enough to potentially break the transitions.  You might also want to give a little more time to your timer to allow Android to get the display rebuilt.

Rob

Thanks Rob, in playing around with tagging I came across the solution that works for the simulator: my original test case was buggy as timer.performWithDelay passes an event object into the transition.resume function which must have confused the matter. A working test case looks like:

[lua]

local c=display.newCircle(display.contentCenterX,display.contentCenterY,20)

function move()

  transition.to(c,{x=c.x<display.contentCenterX and display.contentWidth or 0,onComplete=move})

end

move()

Runtime:addEventListener(“system”,function (event)

  if event.type==“applicationSuspend” then

    transition.pause()

  end

  if event.type=="applicationResume"then

    timer.performWithDelay(1,function() transition.resume() end)

  end

end)

[/lua]

Thanks for the Android tip, will have to play around with it once I get my hands on a device.