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++; }
//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++;
}
}
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();
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<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.
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.