Escape Theory Gaming For Life… views are mine, and not affiliated with The White Agency or Nomad

27May/101

Tween Class Tweens Freezing Halfway Through Animation

When creating multiple Tween instances using the Flash Tween classes, animations randomly freeze.

Error type: Bug

Error class: -

How to Induce error:

Using the built-in Flash Tween classes, create multiple tweens on one or more objects.

When these tweens are all firing , Flash randomly 'cleans up' the tween instance while it is still animating. In effect, the animation appears to freeze.

Further tween instances can unfreeze the animation, but clearly this is too annoying to ignore!

Solution:

We can store the tween instance in an Array, and clear the instance when the tween is no longer required. This forces the Flash garbage collector to stop destroying your tween instance:

var tweensArray:Array = []; var tween_count:Number = 0; function tweenFunc():void{ tweensArray[tween_count] = new Tween(myObj, "alpha", Strong.easeOut, myObj.alpha, 1, 0.5, true); tween_count++; }
function fadeContent(showWhichNumber):void{

//tween class has FREEZING bug, it removes tweens before they complete when many tweens occur - this is RANDOM.
//in order to stop the removal, we store the tweens in an array, and wipe them out on each changeContent call.
//remove this fix at your own risk!

if(currentContentNumber == showWhichNumber){
tweensArray[tween_count] = new Tween(this['content'+ (showWhichNumber).toString()], "alpha", Strong.easeOut, this['content'+ (showWhichNumber).toString()].alpha, 1, 0.5, true);
tween_count++;
}else{
tweensArray[tween_count] = new Tween(this['content'+ (showWhichNumber).toString()], "alpha", Strong.easeOut, this['content'+ showWhichNumber].alpha, 0, 0.5, true);
tween_count++;
}
}

25Jan/102

SWFObject Error passing URI’s and HTML code into flashvars

When passing URL's or HTML code through SWFObject flashvars, some characters cause errors.

Error type: Pitfall

Error class: -

How to Induce error:

Flash file, published as 'example.swf', 550 x 400:

//in timeline
import flash.text.TextField;
import flash.display.LoaderInfo;

//this is for Flashvars
var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
var myTextField:TextField = new TextField();
myTextField.width = 400;

if(paramObj['testVar']){
        trace('found flashvar');
        myTextField.text = paramObj['testVar'];
}else{
        trace('no flashvar found');
        myTextField.text = 'no flashvar found';
}

addChild(myTextField);
stop();

HTML:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>swfObject demo</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body bgcolor="#ffffff"> <div id="flashcontent"> This text is replaced by the Flash movie. </div> <script type="text/javascript"> var so = new SWFObject("swfObject_flashvar_error.swf", "swfObject_flashvar_error", "550", "400", "8", "#ffffff"); so.addVariable("testVar", "Lots & lots & lots & lots of ampersands"); so.write("flashcontent"); </script> </body> </html>

Ensure that swfObject.js is in the correct location (same folder as the .swf and .html in the above example).

If you are getting the 'no flashvar found' message, make sure you are not accessing the .html through your file system (you will know this if the start of your URL is 'file://' or something similar).

We expect to see the words 'Lots & lots & lots & lots of ampersands.' in the Flash, but if you have the correct file structure on a live environment, you'll find that you can only see the word 'Lots', this is because SWFObject doesn't like the '&' character.

This poses a huge issue with words encoded in different languages, and URL's which typically require several non-standard characters

Solution:

Simply use the encodeURIComponent() function in the HTML on your flashvar values:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>swfObject demo</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body bgcolor="#ffffff"> <div id="flashcontent"> This text is replaced by the Flash movie. </div> <script type="text/javascript"> var so = new SWFObject("swfObject_flashvar_error.swf", "swfObject_flashvar_error", "550", "400", "8", "#ffffff"); so.addVariable("testVar", encodeURIComponent("Lots & lots & lots & lots of ampersands")); so.write("flashcontent"); </script> </body> </html>

The encodeURIComponent is available for all Mozilla-style browsers, Safari, Opera, Firefox, Chrome and IE versions 5.5 and above. For more information on this workaround, visit the SWFObject FAQ.

22Jan/100

Applying TextFormat not working

When assigning your textField with a TextFormat, it is not reflecting your new format, this is a simple ordering problem.

Error type: Pitfall

Error class: -

How to Induce error:

//in timeline
import flash.text.TextFormat;
import flash.text.TextField;

var myTextFormat:TextFormat = new TextFormat('Arial',11,0xFF0000);

var myTextField:TextField = new TextField();

myTextField.text = 'My text';
myTextField.defaultTextFormat = myTextFormat;

addChild(myTextField);
stop();

By looking at the code, you would expect the text 'My text' to appear red, but it doesn't. In fact, it doesn't seem like your TextFormat is working at all.

You would probably spend several minutes up to an hour playing around with embedding fonts and eventually settle for copying the Flash IDE example and adjusting it instead without knowing the simple cause of the problem.

Solution:

The solution is to simply assign your text formatting after initialisation and before adjusting its values:

//in timeline
import flash.text.TextFormat;
import flash.text.TextField;

var myTextFormat:TextFormat = new TextFormat('Arial',11,0xFF0000);

var myTextField:TextField = new TextField();

myTextField.defaultTextFormat = myTextFormat;
myTextField.text = 'My text';

addChild(myTextField);
stop();
19Jan/100

Pitfall when passing Null as Number type

Be mindful when interpreting the Null value as a Number, use NaN when it is more suitable.

Error type: Pitfall

Error class: -

How to Induce error:

//in timeline
var myNullValue = null;

if(myNullValue&lt;10){
 trace('null is less than 10... wtf');
}else{
 trace('null is not less than 10');
}

trace(parseInt(null));
//returns 'NaN'

stop();

Running the code above, you will see that Flash appears to interpret the value 'null' as something less than 10. This is not usually what we want. Furthermore, when you try to force null into a Number using parseInt or parseFloat, it returns NaN.

Solution:

To avoid errors, use NaN if the variable supports the value, or test for the opposite if your variable doesn't support NaN.

8Jan/100

TypeError: Error #1009: Cannot access a property or method of a null object reference.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Error type: Run-time error

Error class: TypeError

How to Induce error:

//in timeline
var blart:MovieClip;

//this will induce the runtime error 1009
trace(blart.name);

stop();

This error is caused when you request the property (example above is 'name') of an object that hasn't been instantiated yet. Note that this same error will occur whether or not the property exists in the object class:

//in timeline
var blart:MovieClip;

//this will induce the same runtime error 1009
trace(blart.FOO);

stop();

Solution:

ensure that the object is instantiated in run-time:

//in timeline
var blart:MovieClip = new MovieClip();

trace(blart.name);

stop();

Notes:

This is a popular error that costs a lot of time in solving. Because the error is so simple to fix, the problem is not always with the immediate code. Check the processes leading up to the instantiation of your object, and create smart checkpoints to make sure your object is created before requesting the required property.

If the code is at a point where restructuring the instance methods are difficult, you can work around the error by referencing the object through array notation. The code below would normally trigger the error, but because we use array notation, the error is skipped:

//in timeline
var blart:MovieClip;

//this will NOT induce the runtime error 1009
trace(['blart']['name']);

stop();

The workaround is not recommended because your subsequent code will need to account for both valid and invalid object references! Use at your own risk.