Solved

Error handling in Flash CS5 application

Posted on 2011-03-01
10
349 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
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,…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now