Browsing articles in "ActionScript"

Lazy Number manipulation (such as decimal places or precision)

If you want to manipulate a Number Object (variable) to a specific precision or to fixed decimal places, instead of manipulating the Number itself you can use the Number Class conversion Methods such as toFixed and toPrecision and cast the String Objects they return back to Number Objects:

pi = Number(Math.PI.toFixed(2));
trace(pi); // 3.14

Not a replacement for a dedicated rounding function for mathematical formulae, but a handy shortcut to output rounded Numbers in a DataProvider for example.

p.s. Math.round rounds to Integer Objects, as do Math.floor and Math.ceil which is why this can be useful

Variable assignment by value rather than by reference in ActionScript 3 for Arrays

Something that has bugged me a few times, but I needed to figure out this time, as a Flash beginner, is that because everything is an object (and thus an instance of a class, including primatives) you more often than not (unless using primitives/statics) bind everything because you’re always passing variables by reference.

Coming from a Basic/C/Perl/PHP background where you can pass things by either value or reference it took a while for me to figure out how to just copy an array! Since $array2 = $array1; in PHP for example would copy array 1 to array 2, in ActionScript it will bind array 2 to array 1, and weirdly there seems to way (I can find yet) to pass objects by value. But having found the concat function:

array2 = array1.concat();

http://www.kirupa.com/forum/showpost.php?p=2230759&postcount=4

… you can create a copy of array 1 by returning a new Array object with identical contents. Having seen some deep copy examples I’m assuming that most objects come supplied with copy methods so I don’t think it will be a problem in future, but something that newbies to the language may need to be aware of.

Expecting leftParen before something where there is nothing?

An ActionScript Compiler bug that I discovered today (that has been open for 5 years according to Adobe! http://bugs.adobe.com/jira/browse/ASC-2555;jsessionid=0DF58E72CC5A07E2AF4CAC97C7A22005?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel) whereby a compliation error caused in one timeline script will cascade to another without an appropriate warning as to where it was generated.

In other words you may get a compilation error that states you are missing a parenthesis or similar before the first line of code in a layer of your timeline script, which of course makes no sense. However since all layers are compiled into a single pseudo-document class at compile-time (even if you have a Document Class) as far as it’s concerned the preceeding frame containing a script is glued onto it, so the error will have been generated from a timeline script in a layer higher up.

Call to a possibly undefined method addFrameScript

I decided that I should start posting some of the little niggles that I come across everyday, and the solutions that I’ve found, so that other people can find them somewhat easier than I may have (although that means years of PHP problems have been lost in the mists of time!)

addFrameScript is an undocumented method (does not appear in the ActionScript Language Reference) that allows you to *something *something as explained well at troyworks, AS3: MovieClip.addFrameScript

Essentially if you have a Document Class (if using external ActionScript files as opposed to Timeline scripting) that extends Sprite instead of MovieClip, you won’t be able to use Timeline scripting if *something *something

(I will figure this out properly later, but I’m at work now!)