Animation speed doubles every level

Hi,

I have a simple game that basically consists of one level (script) and a number of groups with two images that animate across the screen. At the end of the level I stop the animation (this is visible), reset all the variables and add one item, and start the animation level again which creates the illusion of a next level.

I’ve tried both frame-based animation and time-based animation (Runtime:addEventListener or timer.performWithDelay), but the animation speed doubles every time a new level is started. I have a fixed very slow speed now (for testing), and the only way I think it speeds up if there are more events (so the animation listener gets called more often).

Any clue on why this might happen, or a workaround? Or a way to see if there are no zombie event generators?

To Corona’s credit, it get’s very fast without problem - most people can’t get it fast enough, which is kind of ironic. This is using the newly released SDK (build 703).

Thanks, Maarten [import]uid: 5822 topic_id: 18926 reply_id: 318926[/import]

I added this to the animation listener and it indeed shows that the removal doesn’t take place:

local newTime = socket.gettime()
local diff = (newTime - lastAnimationTime) * 1000
lastAnimationTime = newTime
print(“animation function called after " … diff … " milliseconds.”)

So a workaround will probably be to do the animation if diff > some threshold. Weird stuff though, no?

[import]uid: 5822 topic_id: 18926 reply_id: 72908[/import]

Hmm, you’d probably need to post a code snippet to see exactly what’s going on. But if the problem is that your listener isn’t getting removed before it’s added again, then the best thing would be to figure out why it’s not getting removed. Otherwise you’ll end up with additional listeners being added after every level, and things will just get messy.

How are you trying to remove the listener?

Thanks,
Darren [import]uid: 1294 topic_id: 18926 reply_id: 72964[/import]

try canceling any timers or transition before the level change
sounds like a timer,transition, or maybe a loop is running which is allowing the new level to execute before the runtime is removed and since the new level is started the previous runtime never gets removed
but would really need to see some code to be sure [import]uid: 7911 topic_id: 18926 reply_id: 72983[/import]

I was struggling with that very problem and what was happening is I had a

Runtime:addEventListener(“enterFrame”, moveCamera)

listener and I was not properly removing it, so when I restarted the level it ran that again and my moveCamera was getting called twice each frame, doubling the speed. When I re-entered a 3rd time, I was 3 times as fast.

Once I solved that my problem went away.

[import]uid: 19626 topic_id: 18926 reply_id: 72999[/import]

All, thanks for replying, but the removeEventlistener gets called as the animation stops. I had the difference in time printed out whenever it was called in the handler, but sadly it kept getting called.

In the next level, you suddenly see that there are two calls on enterFrame (30 fps), one very 32 millisecs, and the other one between 1-5 millisecs, which clearly suggests a deeper problem.

I’ll be traveling for a week then I’ll see if I can make the problem as small as possible without giving all the source away :slight_smile: Anyway, I just keep track of the time elapsed between the last animation and the call, and if the diff > 32 (milliseconds) I animate, otherwise I discard it.

I’ve had my children banging on this small game for a few hours and no problems whatsoever, so the workaround, well, works. [import]uid: 5822 topic_id: 18926 reply_id: 73054[/import]

If you’re certain that the listener is getting removed at the end of each level, then I think the only other option is that the listener is getting added multiple times at the start of the next level. Maybe check for that? [import]uid: 1294 topic_id: 18926 reply_id: 73072[/import]