Setting Up Our Main Class
Get into a rhythm of efficiently setting up your files so you can smoothly transition into writing code. This video shows you how to set up your script for all the code you'll write from here on out.
After creating an ActionScript file, our next goal is to write the code. So, once all file tasks are done, we can start using that file. Before actually beginning to write code, there are a few basic things one must remember while dealing with programming:
- The number of braces that open must be equal to the number of braces that close. Hence, to maintain the balance between these braces, we must close every brace as soon as we open it. Thereafter, the code will go within these braces – this helps avoid any errors that might occur when there's a brace imbalance in a program. (02 Tip: In AS3, you can press Alt-quote and Flash will highlight all the code within the closest brackets: this can be a life-saver!)
- We must import all the required library files inside the section of the package. We must also include the parent class, which can be directly used in the program without rewriting it because of the overriding and overloading concepts.
- The main code is written in the Main() constructor function. Also remember that a function name is always followed by open brace '(' and a closed brace ')'. These parenthesis can either be empty or we can pass arguments through them. However, the concept of passing arguments into functions is a topic to which we'll dedicate an entire title, in later chapters of this series that cover working with functions.
Now, once you complete the code inside the main function, you must save the file in the source folder. The process of saving files in source and deploy folders has already been discussed in previous videos. You can take a quick peek at the previous videos to refresh your memory. One new thing here is that you must save your file with the same name of your main class. Moreover, the first letter of the class name must be in uppercase.
Now, we have two saved files: main.fla and Main.as. Hence, we must create a link between these two files:
Be sure you're in your main.fla file, then (if the properties panel isn't open,) select the window menu and click on the properties option.
When the properties panel is open, you can find the “Class†box. Type in the class name you just created without any extension (aka 'Main').
Finally, click on the small pencil next to the box: if your class editor opens, that means you did everything right. Way to go!