Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Error handling in Flash CS5 application

Posted on 2011-03-01
10
357 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VLC media player with Delphi XE8 3 827
Can not play videos on sciencechannel.com in Chrome 13 139
How to extract back songs to MP3 files 19 112
mpeg to mp4 free tool 8 90
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to record live broadcast.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

838 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