First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Aditya Sharma aka adit_ya_sharma | Website: http://www.ctoffakes.tk
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
(10 being the highest)
» Author
Iam a flash actionscript programmer.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

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

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.