AS3 Tip

Subscribe to AS3 Tip 8 posts, 5 voices

Sign in to reply


 
avatar for Gunstar_Hero Gunstar_Hero 351 posts
Flag Post

For those using ActionScript 3.0, you may have noticed that _root, or root as it would be without the underscore (as most AS3 terms are) doesn’t work properly. It usually returns an error. Now, as a work around, and I’m not sure if it will work for everything, but it works in the terms of saying something like


root.MCNAME.scaleX+=5

or something along those lines, you can go to File > Publish Settings > Flash (it’s a tab) and near the words Actionscript 3.0 settings, there should be a button. Press it. Uncheck the boxes that say “Strict Mode” and “Warning Mode” (Or something near that text) and tada.
root works!
Now, if you wish for this to be automatically applied to all documents you make in flash without having to do this manually, export the profile (there’s a small button at the top) to default.xml and save it. Done.
Tested on Flash CS4 – AS3

 
avatar for Balidani Balidani 254 posts
Flag Post

Why would I not want strict mode? :ll

 
avatar for Gunstar_Hero Gunstar_Hero 351 posts
Flag Post

Well, basically, it allows you to use things like root. That way, instead saying something like


this.parent.parent.MC

you could just say

root.MC

Kinda useful if you started out with AS2 and did a lot of MC programming.

 
avatar for Jabor Jabor 9704 posts
Flag Post

Both of those ways are pretty bad style.

In fact, I would recommend, if you don’t have experience in OOP, that you stick with Strict, until you can do anything within its constraints. That teaches you good style, and once you’ve got that, you should have the experience to know when it’s a good idea to ignore what is generally good style in favour of what is best in one particular instance.

 
avatar for Balidani Balidani 254 posts
Flag Post

What Jabor said :)

 
avatar for Cervello Cervello 76 posts
Flag Post

Just to add on, I suggest you take a look at the AS3 source of Shoot! and see how they work around the lack of root. This is just a quick fix for a quick port to AS3. Even if you want to port AS2 code, things will most likely run smoother if you took the time and re-did it in AS3 the AS3 way, (that means very little copy and paste) and it’ll all be easier to use AS3 from then on. When in Rome.

In case it’s not clear (it wasn’t for me when I first looked <.<), in AS3 Shoot! they use a static pointer for classes where there will only be one instance of it at a time, and a static array for classes there will be multiple instances of. In the constructor of the class, it simply sets the pointer, or adds to the array, the new instance. That way, you can just say for example Ship.main, where Ship is the actual name of the class and main is the pointer. Or Ship.list[number] where list[] is the array (of pointers, sort of). As opposed to root.instanceName or icky stuff like root["instanceName"+number] which is pretty finicky even in AS2 (I assume there’s a better way but I haven’t stuck with AS2 for long.)

 
avatar for Cloud_9ine Cloud_9ine 2242 posts
Flag Post

How would I access root to gotoAndStop(); or would there be a similar alternative for a game with 3 frames.

 
avatar for Cervello Cervello 76 posts
Flag Post

To control the main timeline, and otherwise have a total equivalent of _root as you would with AS2, you need to use the “Document Class” feature. In CS3, just don’t select anything the Properties pane defaults to the document itself. Document Class is at the bottom left. Make the class some extension of MovieClip and you’ll be all set.

When you set the Document Class you can do the whole static var pointer thing in it’s constructor and have something like JustLikeRoot.main.gotoAndStop(frame); where JustLikeRoot is the name of the Class you put in for Document Class and main is the static pointer. You can also do JustLikeRoot.main.instanceName to access instances of objects without having to give them classes.

Just another thing I picked up from the AS3 version of Shoot.

Sign in to reply


Click Here