Subscribe

  • Subscribe  

Run time error handling in AS3

Posted by Jay | May 8, 2008 | Comment

As most of us Flex or Flash developers are quite aware that the code we generate is often error prone; error handling is an issue that mandates serious consideration. While talking about errors we need to deal with compile time errors which are primarily due to syntax incorrectness along with run time errors that are primarily semantic errors. ActionScript 3 has built in “Error” and “ErrorEvent” classes to handle both types of exceptions.

Now let us take a closer look at how run time errors are negotiated in AS3. We find that they can of two types

Synchronous errors

When developer can foresee the possibility of errors at a place e.g. file upload or method invocation with invalid arguments. Such error can be resolved at run time using Try- catch – finally block. In this case, special error objects are being created which are subclasses of “Error” class in ActionScript 3 .

Asynchronous errors

When developer is unaware when error will occur.e.g loading data from external link. Flash Player throws an exception; run-time error that creates event and event listeners is the resolution. “ErrorEvent” class builds listener function for specific error event. Event listener (listener function) is analogous to catch block of synchronous errors. AddEventListener() method of application object maps the error event with event listener (listener function) as application is not aware time of error occurrence.

import flash.errors.IOError;

import flash.events.IOErrorEvent;

import flash.media.Sound;

import flash.net.URLRequest;

import flash.display.DisplayObject;

import flash.events.*;public class Error_Event extends DisplayObject {

private var mp3_play: Sound = new Sound ();

// error handling try catch error event listener

   private function playMP3(mp3_sing:String):void

   {

try{

         mp3_play.load(new URLRequest(mp3_sing));

         mp3_play.play();}

catch (err1:Error) {

         trace(err1.message); // generates error message in error object 

      }

//asynchronous error handling

      mp3_play.addEventListener(IOErrorEvent.IO_ERROR, error_Handler);

   }

private function error_Handler(error_Event:IOErrorEvent):void

   {

      trace(error_Event.text);

   }

}

Using KeyCodes in ActionScript 3

Posted by Jay | May 7, 2008 | Comment

When I started getting a hang of new stuff introduced in AS3, I got to notice that it has done away with a bunch of commands that were available in AS2. One of these missing commands included “Key.isDown” and added too that there were simply no key objects in AS3. Instead it has an alternative process whereby key codes can be accessed by name basically utilizing the keyboard class.

Now lets take a closer look; in AS3 in “Key.isDown” is basically an event handling process which is made out of a two step process

  • Write an event handler (listener function in AS3), event handler is a function which executes in response to the event e.g. key press.
  • In AS3 event has also a class representation per se “KeyboardEvent”. We use the addEventListener() method to map listener function with the target of the event e.g. key press which is an object of “KeyboardEvent” class.

To elaborate the above concept of keyboard event handling Actionscript3 has build in “KeyboardEvent” and “keyboard” class. keypress event can be elaborated as

  • Mapping the key press event with the listener function e.g.
   addEventListener(KeyboardEvent.KEY_UP,keyHandlerfunction) 

Or

   addEventListener(KeyboardEvent.KEY_down, keyHandlerfunction)

Here KEY_UP and KEY_DOWN are constants for respective key up or key down events. Key down event takes place when a key is pressed and reverse Key up event takes place when a pressed key is released.

keyHandlerfunction function will be executed in response to key up or key down event.

package
{
   import flash.display.DisplayObject;
   import flash.events.*;

   public class Event_Example extends DisplayObject {
     private var child: DisplayObject = new DisplayObject ();

    // register the listener function
    public function Event_Handle_Example()
    {
       addChild(child);
       child.addEventListener(KeyboardEvent.KEY_UP, keyHandlerfunction);
    }

   // listener funtion
   private function keyHandlerfunction (event:KeyboardEvent):void
   {
      trace(“keyHandlerfunction: “ + event.keyCode);
    }
}

Save memory and loading time when creating bitmaps with AS3

Posted by Jay | May 6, 2008 | Comment

Often we notice that in our zeal to develop appealing web sites and RIA that look perky we may end up designing pages that take inordinately long to upload. Often stylish web pages necessitate the usage of a variety of object including images which take a serious toll on the memory. ActionScript provides developers with the desired flexibility to play around with the web objects. It allows for methods for keeping memory usage under check so that it takes less loading time taken for a web application.

At times we may find that a same bitmap image needs to be replicated at more than one place. If separate objects are being created for each copy then it becomes an overhead on memory. To avoid such issues ActionScript has inbuilt “BitMap” class for representing bitmap images which allows greater flexibility to developers.

The basic constructor for the bitmap class is

Bitmap (bitmapData:BitmapData = null, 

        pixelSnapping:String = “auto”, 

        smoothing:Boolean = false);

Essentially a Bitmap object can effortlessly share its reference several such objects. Moreover such sharing is independent of any specific property such as translation.

Now when we need replicate the bitmap, we can create an object of replica where constructor of that replica will create a reference to other or original BMP image , thus a great check on memory overhead. This shown below

var copy1: Bitmap = new Bitmap (Bmpcopy .bitmapData);

How to add isPlaying Property to Movie Clips with AS3

Posted by Jay | May 5, 2008 | 1 Comment

Web designer are always looking for means to makes their sites eye catchy and thereby videos and movie clips etc are often placed to jazz things up. To make […] Continue Reading…

Flex Programming Tips of the Week 0805a

Posted by Charles | May 4, 2008 | Comment

A lot of interesting experiments are going on with Flex especially in animations and graphics. This week I thought giving a few of them a closer look along with […] Continue Reading…

Flash Lite + Java ME = Cool Mobile Platform

Posted by Charles | May 3, 2008 | Comment

In an exiting development Sony Ericsson has been able gel two of most popular development platforms in mobile computing through its new technology called ‘Project Capuchin’. It intends to […] Continue Reading…


Msn Display Pictures
Cox television ||| Verizon Bundles ||| Comcast cable tv