Flashplayer Throttle : Solving a problem or creating one !

This might be a little late, but worth keeping an eye on. As we know since Flashplayer 10.1, the player is throttled in situations. What does that mean is the FlashPlayer will run the EnterFrame and Timer based events at much lower rates, when the player is not visible. Much lower means, as low as 2 FPS (2 frames per second) and AS2 Intervals as well as AS3 timers will low down to 2 events per second!!

This throttling is best for mobile and other devices which can run FlashPlayer now. Throttling means saving of energy and increasing battery life of the device. So it seems its the best thing happened to Flashplayer, while we are making content for the devices.

The problem arises when we create stuffs for desktops or laptops, where FlashPlayer was running since a long time, conventionally. And we still think we can go on developing our applications and games with the same mind set as we used to do till now. While developing games, the most used event is ENTER_FRAME and when throttling happens, all of the logic of the application/game goes wrong!! While the logic of the game still holds good if the player is active all the time but once the player is invisible, the player starts to throttle and frame-rates and timer events start to fall. This will happen even our game is running in a browser tab and we are in another tab. This can happen if we scroll down a page and the game is on the top portion of the page! The tricky part is different browsers behave differently and there is no clear idea about when the player starts throttling. So there is no fix solution for the same too.

Continue reading “Flashplayer Throttle : Solving a problem or creating one !”

Global Error handler with Flash Player 10.1

With FlashPlayer 10.1 comes the ability to handle all the exceptions at one place, aka, Global Error Handler. This means a SWF can be made so that it does not pop-up usual error message dialogues, we generally see now a days with a lot many sites, if we have a debug version of the player. This simply does not mean we should ignore any errors in code. What does it mean is, we still have another gate to filter out any error messages from our application. There can be situation in a team environment, where someone can missout something and that will throw an error on runtime. Well, without further digging into the cause, lets dig into our point of handling the errors.
There is basically another property now to the “LoaderInfo” and “Loader” object. This property is known as “uncaughtErrorEvents”. The trick is to listen to “UncaughtErrorEvent.UNCAUGHT_ERROR” event on this property of the “LoaderInfo” and “Loader” object and handle it. Further more, if we completely want to supress the error dialogue box from poping out, “preventDefault()” must be called on the error handler.
Jumping to the code, add an event handler to the keyboard event like
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
On its handler try to remove a sprite from the display list,which is not yet present in the display list. The code is as below
private function onKeyDown(e:KeyboardEvent):void
{
var sp:Sprite = new Sprite();
this.removeChild(sp);
}
At this point, if we try and run our swf and press any key on our keyboard, the error dialogue will come up.(Keep in mind, this only happens if we have a debug version of the FlashPlayer)
Well, now this dialogue is very annoying if we are an user.  We can avoid this by handing the error on our GlobalError handler. In the beginning of the application listen for error on the LoderInfo object of the main application.
this.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
Now, on its handler call “preventDefault()” method of the error event.
private function handleGlobalErrors(e:UncaughtErrorEvent):void
{
//this suppresses the error dialogue
e.preventDefault();
//application specific error handlers goes below

}
The next scenario is when we will be loading external SWFs into our main application. In this case just listen to the Loader objects “uncaughtErrorEvents” property for “UncaughtErrorEvent.UNCAUGHT_ERROR” event as below
var l:Loader = new Loader();
l.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
l.load(new URLRequest(“myOther.swf”));
Thats all, now we have a Global Error Handler in place.
Note :
1. SWFs made for AVM1 with AS2 or AS1 can not be handled for errors on Global Error handler.
2. Even if the Error is handled in the Global Error Handler, the execution of the script does not continue on the call stack that caused the error.
Happy Flashing 🙂