Escape Theory Programming, Flash and Gaming For Life…

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/102

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!

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.