In order to learn Actionscript 2.0, you must first learn some vocabulary. We will examine each of these in further detail. This is just a short list that we will add to as our discussion continues.
Class encapsulates data (variables, attributes, etc.) and methods (functions, methods, etc).
Instance the actual implementation of a class.
Method a function inside of a class that performs a specific operation.
Classes in Actionscript 2.0
When learning, I like to have solid examples of code to work from. So here is your first code from the Product.as file:
class Product {
}
There are some important things to understand about this code:
The name of the file Product.as is the same as the name of the class Product.
You will want to put this code into a package to make it more user-friendly and easier to distribute.
Packages in Actionscript 2.0
A package is nothing more than a folder that contains one or more Actionscript 2.0 classes. Most packages are placed in multiple folders from generic to specific. For example, my package is com.ahfx.Product. I have a folder named com. In that folder I have another folder called ahfx. In that folder I have a class file, Product.as. When I distribute my Actionscript 2.0 classes, I will merely send the folder com to the organization that needs to use my classes. (If you notice you just invert your domain name ahfx.com to get your package name, com.ahfx. Packages also help distinquish between classes with the same name. For example if you wrote a class called Product, we could use the package to tell which Product we really wanted to use). To reflect the package change I will add that to my Actionscript 2.0 class code, Product.as.
class com.ahfx.Product {
}
Build It
We are now going to help you set up your very first Actionscript 2.0 class. We will first create the package, then start Flash MX 2004 and write our first actionscript.
Browse on your computer to your Flash MX 2004 installation folder (for example: C:\Program Files\Macromedia\Flash MX 2004\en
Go to the <Macromedia install folder>First Run\Classes\ folder
In this folder (Classes) create a new folder named com
Open the com folder you just created and create a new folder named ahfx
I would now add a shortcut to this ahfx folder on you desktop.(Just for ease of access).
Open Flash MX 2004
Choose "Create New: Actionscript File"
Copy the code from Product.as above into the new window
Choose File|Save As and save it in the ahfx folder you created above.(If you added a shortcut to your desktop, click desktop, click the shortcut and it will take you to the correct folder).
After saving, press Ctrl-t to check your syntax (Hopefully everything will be fine). If you get errors, make sure your file is named the same as the class name, and that you placed the file in the correct folder.
SUPPORT THIS SITE If this tutorial helped you, show your support and send me something from my wish list. Click on an item below and then choose Adam Hayes (Gift Registry Address) for the Ship to Address. It is that easy!