Tweens paused after losing focus - workaround?


This topic has been brought up before, but I couldn’t find a solution to this problem.

I want my tweens to continue even when the tab is not focused.

I’ve read that this behavior comes from the browser and not Phaser, but then I ask myself why Phaser doesn’t do something about it itself as soon as the focus is back.

Could someone give me a tip on how to work around this behavior? I’m doing an MMO and delayed tweens would be very annoying.

I would appreciate help.

You can catch the refocus, count the lost time, and then seek ahead the tween, maybe.

Can you help me understand this use case? I hear this request from time to time and I just don’t get it.

I use tweens to move the avatars. When you click on a position, the avatar should walk there.

Do you have an example of how to seek the tween when focus is regained? The toPosition field in seek is a value from 0 to 1, how should I calculate this?

Seek value should be something like (tween.elapsed + missingTime) / tween.duration.

Also here’s crazy way to do it:

This is definitely better than seeking tweens and it works like a charme, thank you!

However, I wish it was possible in Phaser itself without having to override resetDelta.

Don’t use that with physics, though. :wink:

I noticed that animations play at a high frame rate after the game had no focus for a long time. As if it made up for the frames lost.

@samme Do you have any idea how to fix this?

There may have been a bug fix for that.

So do you think it will be fixed in the next update?

Thanks for the answer.

Edit: If you mean the Animation.createFromAseprite fix, I’m talking about normal spritesheet animations.

Maybe I was thinking of #4232. Not sure if relevant.

skipMissedFrames is already enabled by default, so I think it must be something else. The longer the focus is gone, the longer the animation plays at a high frame rate when focus returns.

I think that may be unavoidable. You can override AnimationState#update() and remove safetyNet.

I disabled skipMissedFrames, but then the animation keeps playing at a higher frame rate when focus returns.

The missed frames should not be played at once, but a jump should be made to the actual frame.

Edit: I played around with the Phaser source code and removing safetyNet (especially the safetyNet < 60 part) indeed fixed the problem. Should I report this on GitHub or does safetyNet have a reason?

No, safetyNet has a reason.

Ideally you would avoid the time step hack and adjust the tween timing instead.