A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11337 Flash Movies | 1 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Bugra Ozden
» Title: Skatalog v9 - product catalog
» Description: Create your product catalog easly and publish on your website or Create your image gallery, documents list, portfolio. Fully XML Driven
» More by Bugra Ozden


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 5828 Flash Sites | 0 New Flash Links
What's New | Top 100 Flash Site

Featured Site

» Posted in the Flash Kit Links section
» Title: Creative DW Image Show PRO
» Description: Creative DW Image Show PRO is a Dreamweaver extension which enables the user to create multimedia presentations. It combines the features of the popular Creative DW Image Show with the ability to add professional text effects to slides (similar to After Effects). The product is very customizable: the user can choose the duration of the transition effects, the slide motion start and end position, zoom and panning type for both images and texts.


Random Links | Add your own Flash Related Links
Flash Tutorials 1280 Tutorials 7 New Tutorials Added!
What's New | Top100

» Make a Flash Slide Show Screen Saver
» Simple flash making tutorial for thanksgiving
» Create flash banner for website
» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» Unknown Tag: Title10
Random Tutorial | Add Site

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Actionscripting/OOP

Categories Creating Simple ActionScript 2.0 Classes
Author: Kortex | Website: http://www.visualflowdesigns.com |

 
Page 5
«prev 1 2 3 4 5 6 7 next»

'

Examining The Class File:

So by now you have probably noticed that class files are just external ActionScript Files with a specific structure. Let's take a closer look at that structure.

The Class File:


Here is the whole file again.

class Person {
var _myname:String
function Person(myName:String){
_myname = myName;
trace("New Person");
}
function sayHello():Void{
trace ("Hello, my name is " + _myname);
}
}

Class Definition:

Everything that makes up a class is contained between opening and closing brackets { }. The name of the class is preceded by the keyword class. So an empty class named person would look like:
class Person {
}

Since this is a simple tutorial, we have created our class inside the root of the class path that we added. You can further breakdown your class path into folders called packages, but I will not go into that in this tutorial.

Instance Variables or Properties:

Shortly following the opening bracket of the class, we see the following line:
var _myname:String

This is an example of an instance variable or class property. You can have multiple instance variables, but for simplicity's sake I just used one. Instance variable hold information about the state of the class. Each instance of the class (more on this in a bit) gets copies of these variable. The fact that each instance gets copies allows us set different values for these properties for each instance (we actually did this in our example, but I will explain this more shortly).

Also notice that I have not actually assigned value to this variable yet. And notice the :String. The:String is an example of a concept called variable typing. I will not go into this in great detail here, but just know that when I type a variable it can only accept values of that type. So in this case _myname can only be a string.

Finally, notice the _ at the beginning of the variable name. It is a common practice to put an _ before instance variable names to distinguish them from other variables.

The purpose of instance variable should become clearer when we examine the code we used in the class_basics.fla.

Constructor Function:

Following the instance variable, we see a function with a name that matches the class name:
function Person(myName:String){
_myname = myName;
trace("New Person");
}

This function is known as the constructor and is automatically called when the class is instantiated. Instantiated simply means I create an instance of the class using the new keyword (e.g., new Person();). In our example we pass something called a parameter to the function. A parameter is anything that appears between the () when you call a function. In our case we pass a string and use that value to set the _myname instance variable. Please note the following regarding constructors:

  • The common use for constructors is to set the initial state of the class instance (commonly this is setting instance variable values or calling initialization functions/methods). Basically, this is the set up function.
  • If you do not need, or want, anything to happen when a new instance of the class is created, you can supply an empty constructor (one that does nothing when it is called), or you can leave it out completely. However, in our example that would not work well as the _myname var would never be set. Hence put some thought into whether the construtor is need and what it should do.

Methods:

Finally, at the end of our class we see:
function sayHello():Void{
trace ("Hello, my name is " + _myname);
}

Functions within a class are referred to as methods. While the constructor method is called automatically, other methods within a class have to be specifically called (though you can call other methods from within the constructor). To call a method of a class instance you use the following syntax:

instanceName.methodName();

You will see examples of this when we go over the code from the class_basics.fla in the next section. These methods can also accept parameters, in which case, the syntax would look like:


instanceName.methodName(parameter);

Summary:

So that concludes our brief look at the structure of a simple class. There is a lot more that can go into a class file and there are quite a few things that I did not explain, but for now, let's move on to exploring what happened when we used the code in the class_basics.fla.

'

«prev 1 2 3 4 5 6 7 next»

» Level Intermediate

Added: : 2007-02-20
Rating: 8.93 Votes: 26
Hits: 1656
» Author
Kortex (aka Jeremy Wischusen) is a Flash/PHP developer for myyearbook.com and the lead software architect for the DigitallyU Digital Portfolio Software Suite.
» Download
Download the files used in this tutorial.
Download (46 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs