Link to home
Start Free TrialLog in
Avatar of richo22
richo22Flag for Australia

asked on

Error handling in Flash CS5 application

I have built an application in Flash CS5. The application responds to button clicks and opens external SWF files as required. Very occasionally (less than 1%) the SWF file does not open when the button is clicked, and the application stops working. The user cannot interact with it, as the home button no longer responds. The only option is to close the program and then reopen it.

Is there any error and exception handling code that I could incorporate into the program so that the user does have to close the program and start again?
Avatar of deepanjandas
deepanjandas
Flag of India image

This is a common problem in Flash based applications if the code is not written with a view to improve performance.
The most common of them are are as follows:
1. Using event handlers wisely and clearing them when-ever they are no longer required.
2. Removing object from stage whenever they are not in use and after removing set that variable to null for removing it from the memory of the flash player.
3. Try using try..catch where ever possible in your code

Apart from those kindly go through these articles which will help you to improve the overall performance of your flash application.
https://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/A_4195-Tips-Tricks-and-Best-Practices-for-Improving-ActionScript-Performance.html
https://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/A_2107-20-Tips-to-Optimize-your-ActionScript.html

Also if you can share your file, might be able to provide some suggestions as well.

Warm Regards
Deepanjan Das
Avatar of richo22

ASKER

Thanks for getting back to me so promptly. I'm going through the two articles you suggested. Also I have a few other comments.

1. I am removing event handlers as soon as they are no longer required. Great advice.

2. I have a question about setting variables to null after removing them from the stage. Let's say I have an object called Home Button. I also have two functions - addHomeButton and removeHomeButton.

var homeBtn = newHomeBtn();

function addHomeButton():void
{
     addChild(homeBtn);
}

function removeHomeButton():void
{
     removeChild(homeBtn);
     homeBtn = null;
}

So far so good. But next time I want to add the homeBtn and call addHomeButton(), the variable has been set to null, and so it cannot be added. But if I declare the homeBtn variable inside the addHomeButton() function, then the second function, removeHomeButton() cannot see it.

Is there a way to solve this? Am I missing something?

3. Could you point me in the right direction to learn how to use try..catch statements?

All the best,
Richard Galbraith

You are right, so you also need
 
var homeBtn:HomeBtn = new HomeBtn();

function addHomeButton():void
{
     if( homeBtn ){
         homeBtn = new HomeBtn();
     }
     addChild(homeBtn);
}

function removeHomeButton():void
{
     removeChild(homeBtn);
     homeBtn = null;
}

Open in new window


Warm Regards
Deepanjan Das
Opps, the correct one is:
var homeBtn:HomeBtn = new HomeBtn();

function addHomeButton():void
{
     if( homeBtn == null ){
         homeBtn = new HomeBtn();
     }
     addChild(homeBtn);
}

function removeHomeButton():void
{
     removeChild(homeBtn);
     homeBtn = null;
}

Open in new window

Avatar of richo22

ASKER

Works a treat. Thank you. This will help me with memory management issues.

How about error handling. Can you point me in the right direction to apply try..catch statements?





where-ever you are trying to do any action like load, unload, etc. use try ...catch there:

try{
     loader.load(.....);
}catch( error:Error ){
     trace(error);
}

Warm Regards
Deepanjan Das
Avatar of richo22

ASKER

OK, great. I'll try that.

Now just to confirm that I'm on the right track with memory management.

Up to now, I've been declaring my variables at the head of the program, and then adding and removing from the stage as required.

Thus, when working with an object called RedButton:

var redButton = new RedButton();

addChild(redButton);
removeChild(redButton);

But with around 500 variables, this is taking up a lot of memory at program start up, whether the variables are used or not. Now I'm doing the following:

var redButton:RedButton;    //declare the variable at the head of the program, but leave it null

and when required later on...

redButton = new RedButton();
addChild(redButton);

removeChild(redButton);
redButton = null;                //Which clears the memory until redButton is needed again

Does all that sound OK?
ASKER CERTIFIED SOLUTION
Avatar of deepanjandas
deepanjandas
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richo22

ASKER

Thanks for your help with this one. I'll work my way through the code to remove variables when no longer required, and have a crack at error handling.

I may be back for help on try..catch, but for now...thanks!

Would you be interested in seeing the finished product some time?
Sure, why not, would love to. Cheers

Deepanjan Das