Actionscript 3.0 Basics: Working with a class
By admin • Jul 11th, 2009 • Category: TutorialsAs you know I decided to start to make my transition from AS 2.0 to AS 3.0. On my way I've decided to cover a series of tutorials about the basics of actionscript 3. I will mostly be using very simple language and it would be easy to follow if you've already worked on AS2, although it shouldn't be too hard if you're just starting to work on flash.
The first thing I noticed was that while using AS 3.0 it would be much more preferable and cleaner to put code in separate class files rather than the timeline. So here I’m providing a simple introduction to class files.
STEP 1: Go to File->New->Actionscript File

STEP 2: Type the following code into the file. (It will be explained in due time)
-
package{
-
import flash.display.*;
-
import flash.text.*;
-
public class Myfirstclass extends MovieClip {
-
public function Myfirstclass() {
-
var objectVariable1:TextField = new TextField();
-
objectVariable1.text = “Welcome to Classes”;
-
addChild(objectVariable1);
-
}
-
}
-
}
Explaination of the above code:
LINE 1: It declares that this Actionscript file contains a package that defines a class.
LINE 2: Imports a “Library Class” flash.display.* (ie. All functions in flash.display)[If you don’t know what a library class is just google it.]
LINE 3: Imports another library class for text. We’ve imported these two classes cause in our little program we have inserted some “text” and “displayed” it on the screen.
LINE 4: Defines that this class is public ie. It can be accessed by the MovieClip. The name of the class is Myfirstclass (note: the file must also be saved as Myfirstclass.as). the “extends MovieClip” part is appended so as to tell flash that this class works in unison with a MovieClip (the root is also a movieclip)
LINE5: Defines a new function in the class
LINE6: Declaration of a variable named “objectVariable1” of the type “TextField”
LINE7: Putting the string to be displayed into the textbox.
LINE8: Adding the textbox to the root.
STEP 3: Go to File->New->Flash File(actionscript 3.0)

STEP 4: In the properties window change the document class value to “Myfirstclass”

One compilation the .as file is automatically compiled and the text is shown onto the screen.
You can download the source files for this Tutorial here.
admin is An engineering student from India. Enjoying his life both in the real world and in cyberspace
Email this author | All posts by admin

[...] those of you who are not familiar with how to work on class files, you can check out the tutorial here. Do not forget to link your .as file to your .fla [...]
[...] that you can create a class(shown here) and handle events(as shown here). It’s time for some user interaction. Now in this tutorial [...]