Before we start
For this tutorial you will need the following:
- The Adobe Flash IDE
- Flash Develop (optional but very usefull)
- Basic knowledge of programming
In this short tutorial I will try to explain some of the difference between AS2 and AS3.
Though I do admit this is hardly to be called a tutorial, it's more a summary how classes work.
If you are new to AS3 but you have experience with programming,
I advice buying 'Learning ActionScript 3.0' written by Rich Schupe. This book helped me a lot!
If you are completely new to programming I wouldn't buy this book but try some good tutorial websites like
www.gotoandlearn.com
and start with the tutorials down at the bottom of the website.
Classes and Instances
Lets say you drew a bicycle and converted it to a MovieClip.
When converting you checked the 'export for Actionscript' box and the identifier said 'Bicycle'
The file properties said that you where working with ActionScript 2.
So you could write the code to add the bicycle to the stage
this.attachMovie("Bicycle","Bicycle_mc",1);
Now if you would create a Flash file published with ActionScript 3 this would be completely different.
You would draw the bicycle and then convert it to a MovieClip. Then you would notice the fist difference.
When 'exporting for Actionscript' the field Identifier will be disabled. We are demanded to use the Class, so lets use it and call it a 'Bicicle'
Now when we want to add the clip to the stage we would do it like this:
var bike:Bicycle = new Bicycle();
addChild(bike);
As you can see the first line start by creating a variable bike
The type of this variable is 'Bicycle', other variable types could be string or Number
If we would stop at that point we would only have an empty variable with the type of Bicycle
Think of it as creating an variable witht the type Number without telling what number is really is.
So then follows the ' = new Bicycle()'. This is the only way to create an instance of a class.
When we look at the properties of the Bicycle in the libary we can see that the base class is 'flash.display.MovieClip'
This tells us that our class Bicycle can do exactly the same things a regular MovieClip can do.
So we could add the following code which runs perfect.
-
bike.x = 200;
-
bike.y = 100;
Altough it seems really obvious to be able to set the x and y position, it is a good example of a class being extended.
In this case our empty class Bicycle is being extended by MovieClip, therefore we can use all of the functions and properties the MovieClip class has.
Custom classes
If we would like to give our bike some more functionalities we could create a custom class for it.
Locate you fla file, then make a folder called 'Bikes', in there we create an actionscript file (.as)
There are a few things default about making a class:
package bikes{
//the package is allways required
import flash.display.*;
//imports are optional but almost allways needed.
public class Bicycle{
//this is where the class starts, this is allways required
//this would be the best place to define all the needed variables within this class
public function Bicycle(){
//this is the constructor, this is not required
}
}
}
The first line tells us where the file is located in relation to the Fla file.
The syntax for a deeper folder hierachy is dot seperated, so if this file would be in /bikes/mountainbikes/
'package bikes.mountainbikes' would be correct
After telling the package we define which pre-built classes we are going to use.
The import flash.display.* enables us to use all of the display types flash has.
If you need a file of your own it could look like:
'import /bikes/standardbike;' where standardbike would be a actionscript file in the folder bikes
Then we see the 'public class Bicyle'
When defining a class it should allways start with 'public class' then a name you can come up with.
Then there is the line where you should place all the variables needed inside the class
For example, our bike probably needs a variable speed. you should place 'private var speed:Number' at this location.
When defining a variable or function as private it will only be available within the class,
When defining a variable or function as public it will be available to every class when it has an instance of this class
At last there is the constructor, this is a function with the exact same name as the class name
When an instance is made of this class the constructor function is being called directly.
So when the stage says '... = new Bicycle();' the constructor of the Bicycle class is called.
The last step for making our class work is changing the properties of the MovieClip in the libary
The Class field should contain the package and the Class name, in this case: 'bikes.bicycle'