How to fix game freezes after showing an Ad
After showing an ad to the user the game crashes. Or at least this is what it looks like. The truth is however, that the game just „forgot“ to render itself.
The error can be forced by using this test: Open the game in the stock browser. Now change to another tab and then go back to the game tab – the will game freeze too.
So why does this happen?
When going back to the game tab, the game should get a „focus“ event from the browser. Unfortunately. on these devices there is no function in the stock browser, that is listening for that event. In order to make the game work, you have to implement this function by yourself:
That’s all. Adding these three lines of code to your game ensures the game will work as expected.
The requestAnimationFrame(callback); is triggered in the code. You have to find it and change the „callback“ to the right one.
This way you are able to trigger the animation loop by yourself. You can test this fix it by using simple button:
<input type=“button“ value=“kick it!“ onclick=“Game.engine.renderFrame()“></input>
If the game is played on a device that is giving the event in question back correctly, the requestAnimationFrame function is now of course called twice, but this does not cause any problems. You can think of it as a message asking the system, if there is a possibility to render the canvas – and if the function is called for a second time it just gets ignored by the system. Should you ever run into this issue with Samsung phones like the S3 and the stock browser, just implement this code snippet.