Solved

Error handling in Flash CS5 application

Posted on 2011-03-01
10
356 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:richo22
  • 6
  • 4
10 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35014189
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.
http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/A_4195-Tips-Tricks-and-Best-Practices-for-Improving-ActionScript-Performance.html
http://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
0
 

Author Comment

by:richo22
ID: 35014848
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

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35014885
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

by:deepanjandas
ID: 35014888
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

0
 

Author Comment

by:richo22
ID: 35014998
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?





0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35015026
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
0
 

Author Comment

by:richo22
ID: 35015337
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?
0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 500 total points
ID: 35015800
So you need to think here:

Do you need to declare the variable at top, I mean you need to decide if you require that element always. If not then go by the rule - declare only when required.
That means create inline variables within the function and while removing the elements use getChildByName to get hold of the element.
That way you will not require to define or declare all the elements upfront reducing load time overhead.

Warm Regards
Deepanjan Das
0
 

Author Closing Comment

by:richo22
ID: 35016135
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?
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35016173
Sure, why not, would love to. Cheers

Deepanjan Das
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question