samedi 25 décembre 2010

Handling errors in flash cs3

During a presentation about the universe using a flash movie for showing images and video's i got the error:

Error #2044: AsyncErrorEvent non pris en charge : text=Error #2095: flash.net.NetStream n’a pas été en mesure d’appeler l’élément de rappel onXMPData. error=ReferenceError: Error #1069: La propriété onXMPData est introuvable sur .CustomClient et il n'existe pas de valeur par défaut.

These errors in the middle of a presentation are a nuisance and you want to get rid of them!
(On the other hand the children liked very much shouting ERROR ERROR ERROR!)

There are a few ways to get rid of an error:
first write good code, :-)
secondly, if you expect something to go wrong you can use a try catch structure:

var myNumberWhichCanGoWrong : 10;
myNumberWhichCanGoWrong--;
trace( 1/myNumberWhichCanGoWrong);

Although I even remember that flash catches thiseasy error itself, you can do:


try (
trace( 1/myNumberWhichCanGoWrong);
)
catch(e:Error){
trace("heee!, you are trying to divide by 0!!!!");
}

but sometimes this "try and catch" game does not work, like in this case (of my presentation of the universe to children):
you have a video, but it lackes some XMLData which are expected and when you create a netstream connection, even with try and catch:

private function connectStream():void {
            try {
                stream=new NetStream(connection);
            } catch (e:ReferenceError) {
                trace(e);
            }
            try {
                stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            } catch (e:ReferenceError) {
                trace(e);
            }
            try {
                stream.client = new CustomClient();
            } catch (e:ReferenceError) {
                trace(e);
            }
            addChild(video);
        }

 with this class:
class CustomClient {
    public function onMetaData(info:Object):void {
        //trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
    }
    public function onCuePoint(info:Object):void {
        // trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
    }
}

As you can read in the error above:

Flash expects MetaData and if these are not available and there is no default, this results in an error. And "try catch" does not help...

i found the solution here:
http://www.adobe.com/devnet/flash/quickstart/metadata_cue_points.html

it is adding an eventlistener for this error:
.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 

stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 

and then handling this error, or ignoring it in the function  asyncErrorHandler

        private function asyncErrorHandler(event:AsyncErrorEvent):void {
            //trace(event.text);
        }


In FLASH cs3 errors are regularly spoiling the fun, this way of getting rid of your errors by eventlisteners is a good tool to spoil the fun of flash trying to irritate you with errors!

And why was I using Flash to present and not powerpoint?
I is just fun programming your own presenter, and also I use flash to connect to my DIY remote controls, see this blog entry about these electronic wonders:
http://myfablab.wordpress.com/2010/12/23/making-a-diy-remote-control-for-a-performance/