Search tutorials
Steps for .as file and class creation:-
1)Create a new actionscript document (.as) by clicking on 'New' on the menu bar and then selecting Actionscript file and pressing 'ok'. The .as file should be in the same folder as the .fla file.
2)In the acionscript file, we are going to create a class for the functioning of the MovieClip 'Circle'.
3)Since the class name and the .as file name should be the same, save the .as file with the name 'Circle'.
4)Now we create the class like this:-
class Circle
{
}
Note: In AS 2.0, in this example, since I want to move the movieclip to the right, my 'Circle' class should include all the predefined methods of a 'MovieClip' and should be able to use different properties like _x and _y, which are the members of the 'MovieClip' class. Hence for using them, we would use the 'extends' keyword, to include all methods and properties of the 'MovieClip' class into our 'Circle' class. This might remind you of the similar syntax used in java for inheritance.
5)So the class now looks like this:-
class Circle extends MovieClip
{
}
6)As I had mentioned before, a constructor should always be written inside it. The constructor is a function with the same name as the class which is defined inside it. Simply remember, a constructor function is called whenever an object of the class is created. Currently, I'll make a new MovieClip type variable in the class, and when the instance of the MovieClip in .fla file will be passed, MovieClip type variable will be equaled to that instance name for the reference to the 'circle_mc' MovieClip. So now the class looks like:-
class Circle extends MovieClip
{
var circle_mc:MovieClip;
function Circle(passed_mc:MovieClip)
{
circle_mc = passed_mc;
}
}
7)Our basic aim is to move the circle right when we press the Right Arrow Key, so the class should have a function to handle this action. This will be done by making a method (or function) for executing the code for moving the MovieClip, which is specified as Public for access from anywhere in the class. The class looks like this:-
class Circle extends MovieClip
{
var circle_mc:MovieClip;
function Circle(passed_mc:MovieClip)
{
circle_mc = passed_mc;
}
public function moveRight():Void
{
circle_mc._x += 1;
}
}
Note: Look at the keyword "Void". This the return type specified, because the method is not returning any value, it is simply executing a statement.
8)Save your .as document.
Now we will create an object of the class and move the MovieClip... | » Level Intermediate |
|
Added: 2005-10-12 Rating: 7 Votes: 9 |
| » Author |
| Iam a flash actionscript programmer. |
| » Download |
| Download the files used in this tutorial. |
| Download (0 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!