I have been programming actionscript (2.0) for about 3-4 years now, and over the past few years I have received quite a few emails from fans and developers alike, who want help with code. From these emails I have realized that a lot of times, I code quite a bit differently then they do, and a lot of that is because they are:
1.Still new to Actionscript
2.Have learned only from online tutorials/forums
3.Are used to coding with what works instead of what works best.
So I decided to make a post about it:
Good Programming Technique 101 – Coding on the main timeline.
Before you read on, take note that:
This is being coded is Actionscript 2.0 (I still have to fully embrace AS3 although I highly encourage learning it). However you can still apply this to AS3.
Coding on the Main Timeline
The more complex your game gets, you're going to have more movieClips, layers, frames, lines of code, etc. And with that, it will be harder to organize everything. Which is why it is always good technique to code on the main (or _root) timeline. Now some of you may not even know it, but you can actually control every aspect of your game by coding into the first frame of your flash document. For example, on my latest game I had right around 2000 lines of code on the very first frame. Calling movieClip ‘onEnterFrame’ methods from the main timeline instead of on the instance of the movieClip itself can give you a more organized coding layout.
If you are like most beginners, you are very used to coding with:
onClipEvent(enterFrame). This is a common mistake, because there is a better way to do this: Call it from the main timeline.
You can do onClipEvent(enterFrame) outside the movieClip?
Yes, yes you can. And here is how:
On the main timeline for a movieClip with the instance of ‘myClip’ you would have something like:
myClip.onEnterFrame = function():Void{
This would be the same doing a onClipEvent(enterFrame){ on the myClip movieClip, you just have to make sure you have the instance name (in Properties) set to 'myClip' and that it is on the main timeline.
-If it is not in the main timeline, lets say it’s in a movieClip called 'otherClip' you simply have to add a bit of code:
otherClip.myClip.onEnterFrame = function():Void{
Umm ok, but I still don't see why you do this...
Fine fine, I understand this might be a little confusing at first, but in time I'm sure it will make sense; it might take a little trial and error. The reason I say it is good technique is that having all of your code on the main timeline makes it very easy to keep your code organized and easy to locate. If you have several movieClips/frames/layers in your game that you want to apply code to, it’s going to get confusing. Where did I put that clip? Where is the code that deals with the hitTest? What frame is it on? Etc. When you code in one place you don't lose, forget, or repeat code. Its all in the same place so it is easy to work with, and if you ever code with someone else it will make their lives a lot easier because they won't be looking all over for code they don't even know exists.
Alright, so just a few notes to end on:
It is always a good idea to code on the main timeline.
You can save lots of lines of code and better organize your code by calling onEnterFrame functions.
Comments