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.
Class Constructor Parameters
When instantiating a class, we may supply some default values to its variables – these are known as parameters. For example:
class Castle{ var numOfTowers:Number; function Castle(towers:Number = 4){ numOfTowers = towers; } }
The above supplies any new instances of Castle with a default number of four towers.
When instantiating a new Castle, we can alternatively supply arguments to the class in place of the default values. The code below creates a castle with the number of towers set to five:
var myCastle = new Castle(5);
If we do not supply parameters with default values, they will become compulsory arguments; forgetting to supply the argument will throw an error!