Flex / Flash CS3 Warning# 3551

ActionScript Warning: 3551: Appending text to a TextField using += is many times slower than using the TextField.appendText() method.

ActionScript Error Description:
I love this ActionScript Warning. This is not technically an ActionScript Error instead it is a warning that helps you code better. It is very clear and easily deciphered. This states that you should not use the old way to add text to a text field but rather use the new TextField.appendText() method. This improves performance dramatically and will prevent the user from getting the 15 sec. timeout. (Error #1502: A script has executed for longer than the default timeout period of 15 seconds.)

Fix:
Use TextField.appendText() instead of +=.

Bad Code:

var something:String = “Happy “;
var somethingElse:String = “Birthday”;
myTF_txt.text = something;
myTF_txt.text += somethingElse;

Good Code:

var something:String = “Happy “;
var somethingElse:String = “Birthday”;
myTF_txt.text = something;
myTF_txt.appendText(somethingElse);

ActionScript Warning #3551 is simple and easy (the way that all ActionScript errors/ warnings should be)

As always – Happy Flashing

Flash/Flex AS3 Error #1023: Stack overflow occurred.

Error #1023: Stack overflow occurred

at MyClass/get id()

Description:
This will happen if the stack has so much data that it is full. Nothing like Thanksgiving dinner to overflow my stack.  What is a stack and how does it get full you ask. Simply put each method(function) has a stack and that stack stores data associated with the method such as local variables with the method. Although it is a good idea to understand what a stack is you really you don’t need to have an in depth understanding of stacks to understand this error and it’s solution. Basically this error is saying that you are most likely calling the function recursively. Or in other words you might be calling the function from within itself.

Solution:
To rid yourself of ActionScipt error 1023 make sure that you don’t call the method from within itself which will eliminate any type of recursion in your method call. You may be tempted to do this by setting a variable name the same as the function name. Change either one and this will go away. Notice in the ‘Bad Code1’ example that each time the getter returns what you think is an ‘int’ you actually are calling the setter function again recursively. The trace statement will also produce AS3 Error #1023

Bad Code1:

public function set id (myId:int):void
{
this._id = id;
}
public function get id ():int
{
trace(“this.id”,this.id);
return this.id;
}

Good Code1:

public function get id ():int
{
trace(“this.myId”,this.myId);
return this.myId;
}

Bad Code2:

private function recursiveLoop ():void
{
recursiveLoop ();
}

Good Code2:

private function noRecursion():void
{
someOtheFunction ();
}

Adobe actually has a reasonable explanation for this error in liveDocs. Here is a link to the Adobe page that explains ActionScript Error 1023

As always Happy Flashing

Flash CS3 / Flex 2 Error #1502:

Error #1502: A script has executed for longer than the default timeout period of 15 seconds.

Problem:
You have entered the realm of “Flash Infinite Loop” or at least a really long loop. This is very common when running loops, especially do…while loops.  The timeout is now set to 15 seconds (see image below).

ActionScript Error 1502

Fix:
Make sure that you are not executing a loop that will never break out. Those poor loops running in circles for all of us Flashers/Flexers.

*******************Warning*******************
The Bad Code below will send your Flex/Flash into and infinite loop and will potentially crash your Development Tool. DO NOT COPY and PASTE the BAD CODE EXAMPLES BELOW.

Bad Code:
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i–);
trace(i);
}

Good Code:
var myLength:int = 2;

for (var i:int = 0; i < myLength ; i++);
trace(i);
}

The code examples above have the var i starting with a number that is smaller than the number it is trying to iterate to. This code basically says as long as i is smaller than myLength then loop and each time I loop make i even smaller.

Bad Code:
*********This code crashed my Flash multiple times.*******

var myArray:Array = [“joe”, “bob”,”pam”];
var randNum:int = Math.round(Math.random()*myArray.length-1);
var curNum:int = 1;
do {
trace(randNum);
} while (randNum == curNum);

The key to using do…while loops in Flash is to make sure that the variable that you are validating against is changed inside the loop itself. Unless the variable that you are validating against changes within the loop itself it will loop forever. This example of a do…while loop is valid but will very likely put Flash into an infinite loop because the likelihood that the number will be 1 is very high, especially considering that the code is rounding and the number 1 is the middle digit of the three. This loop can execute quickly if Flash happens to choose another number or it can loop infinitely. The reason you might be doing this kind of loop is because you want to pull things out of an array, xml, or some other list and don’t want to get duplicates. A better way to do this is to either use the splice() methods in Flash or Flex or if you don’t want to affect the original array you can slice() out a duplicate without the curNum value and then the value will never be repeated because it no longer exists in the items that you are evaluating against. That way you know for sure the item that you don’t want repeated is gone.

There are many other ways to run into an infinite loop. These are just a couple. Feel free to post any questions you have in the comments section and I will answer them promptly.

Thanks and Happy Flashing.

Flash CS3 / Flex 2 AS3 Warning: #3596: Duplicate variable definition.

Warning: 3596: Duplicate variable definition.

Problem:
You are using the same variable and Flex/Flash doesn’t like it.
Fix:
Change the name of one instance or unnest your loop.

Bad Code:

var myArray1:Array = [“a”,”b”,”c”,”d”,”e”];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var i:int=0; i < myArray1.length; i++)
{
trace(myArray2[i][i]);
}
}

Good Code:

var myArray1:Array = [“a”,”b”,”c”,”d”,”e”];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var j:int=0; j < myArray1.length; j++)
{
trace(myArray2[i][j]);
}
}

or

var keys:String = “Word”;
for (var i:int=0; i < keys.length; i++)
{
//Code
}

for (i=0; i < keys.length; i++){
//More Code
}

Once Again – Happy Flashing

Flash vs. 21,150 Search Engine Keywords

Flash vs. 21,150 Search Engine Keywords

In my quest to build beautiful, exciting, and effective websites with Flash I decided to come up with a list of 20,000+ keywords for submission to Google etc… I wrote a little script that would combine keywords to make additional keywords. I created a few arrays with certain terms that would play nice with other terms. I then ran the script and tried to loop through each Array and combine the terms to create keyword phrases or Google Food as Gregory Cox would put it. I thought that Flash would be able to concatenate the string and push it into a text field on the stage without any problem. It turns out that Flash has a huge issue with writing to the text box each time it updated and ran through the loop.

The match was like Mike Tyson against Michael Spinks or Carl “The Truth” Williams. In less than 95 secs each was easily defeated. I chatted with my Friend and Flash Expert Tyler Wright and thusly took the heavy lifting away from Flash and instead made a variable that would store the data.After the nested loops had finished I throw the variable into the text field.

The result turned everything around. Flash turned back into the superhero that it has always been and kicked it out in less than one second. I kept adding to the array to see how it would handle even more data. I also tried putting the data into a Trace statement in the same way I tried to push the data to the text box. The result was slower than writing to the text field after the loops finish but the fun part about using trace() is that it will only retain a certain number of the results. The “Output window” will retain only 9999 items according to my test. Flash will process the entire amount but will only display the last 9999.

So this is what I learned from this exercise.

  1. Abstract the processing from the display. In other words set up a variable to store the data while looping and then only display that data when the looping is complete.
  2. The output window will only display the last 9999 results from any trace or combined traces.
  3. Flash AS 2 with Flash 8 can not handle repeated updates into a text field with large(really large) strings

Stay tuned for all the Search engine tips and tricks of how to optimize Flash files for optimization