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.
Feedback Sandwiches – Giving Constructive Criticism
As programmers, we take narcissistic pride in our own coding prowess, and unless you and your colleagues are used to constantly reviewing each other's code, you can mistake another developer's feedback as an inflammatory remark.
Similarly, you may not realise that the feedback you give another developer to be interpreted as an attack on their hard work.
This is when we utilise the all-important feedback sandwich.
The feedback sandwich simply contains three parts: Positive feedback, constructive criticism, and more positive feedback - in that strict order.
Timely dishing of feedback sandwiches keeps the morale of your colleagues up, but always ensure that the 'meat' of the sandwich, the constructive criticism, is heaped enough to stick and become digested in any meaningful way.
For example, rather than saying:
'Your nested functions really messed up our application, can you please fix it?'
You should put it in a sandwich between light compliments like:
'I like the way you set-up the classes, but the nested functions are not really the way to go, can you please fix it? I like the naming conventions though.'
Remember, feedback sandwiches make you and your team happy, and when you receive a feedback sandwich yourself, be sure to understand the meat between the bread!
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.
