Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error handling in Flash CS5 application

Posted on 2011-03-01
10
Medium Priority
?
363 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
Suggested Courses

782 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