Script Causing Flash to Run Slowly

– taterboy | July 24th, 2008

Filed under: ActionScript 2, ActionScript 3, Flash, Tips

“A script in this movie is causing Adobe Flash Player 9 to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?”

This error message is commonly caused by a hanging “for” loop. First check the code is written properly with no typos or mix-matched variables.

Here is a sample (AS2):

?View Code ACTIONSCRIPT
for(i = 1, i <= 5, i++){
	//do stuff
}

Next check to see if the loop exit expression (i <= 5) contains a variable for the exit number (5). Many times we will use an Array.length, XML.childNodes.length or other source to tell our loop how many times to repeat before exiting.

Any time the exit number is not an actual number we should protect our loop with an "if" statement. The nature of the internet teaches us that nothing is absolute. We should prepare for the possibility that our swf will load then the server could go down before the rest of our files load. Internet speed fluctuations, caches, and client system issues all add the possibility that our code will not be executed as we designed it. Especially if we are relying on external files or data sources.

Though our code is technically correct, as developers, we should bare in mind the impact on our project if the external XML file we are expecting is misfiled, renamed, corrupted. Note: some servers are case sensitive (Apache) and some are not (Windows), if you are having migration issues, double check the cases of your data calls and source files.

Protecting a "for" loop is as simple as placing an "if" statement around it that evaluates the exit number for value and existence.

?View Code ACTIONSCRIPT
var loopCount:Numer = xml.childNodes[0].childNodes.length;
if(loopCount > 0){
	for(i = 1, i <= loopCount, i++){
		//do stuff
	}
}

Here is a sample of AS3 code:

?View Code ACTIONSCRIPT
var loopCount:Numer = xml.children().length();
if(loopCount > 0){
	for(var i:uint = 1, i <= loopCount, i++){
		//do stuff
	}
}

UPDATE:

It is probably more common to start the loop at 0 then 1, as XML and Arrays are both zero indexed.
This also changes the exit number evaluation from less-than/equal-to (<=) to less-than (<) because XML.length() starts at 1.

?View Code ACTIONSCRIPT
var loopCount:Numer = xml.childNodes[0].childNodes.length;
if(loopCount > 0){
	for(i = 0, i < loopCount, i++){
		//do stuff
	}
}

Here is a sample of AS3 code:

?View Code ACTIONSCRIPT
var loopCount:Numer = xml.children().length();
if(loopCount > 0){
	for(var i:uint = 0, i < loopCount, i++){
		//do stuff
	}
}

Technorati Tags: , , , , ,

| 3 Comments » | facebook:

3 Comments »

  1. It’s generally a rare case that you’ll start at 1 and skip the first item in your Array, XMLList, or whatever. As long as you include the full set and start at 0, network problems or anything else that cause a set of zero items won’t have any problems.

    Comment by Some Developer — July 24, 2008 @ 3:05 pm

  2. Good point, “i” should equal 0 when working from an Array or XML which is very common and what I am using as an example. Starting at one is a bad habit I picked up years ago and has caused a couple other developers to scratch their heads.

    Comment by taterboy — July 25, 2008 @ 6:46 am

  3. [...] recorded first by extremeplayer14 on 2008-12-25→ Script causing Flash to Run Slowly [...]

    Pingback by Recent URLs tagged Run - Urlrecorder — January 6, 2009 @ 1:01 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

*