Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 1 - my first AS 3.0 class

This is a discussion on Tutorial 1 - my first AS 3.0 class within the Object Oriented Programming - tutorials forums, part of the Flash English category; How to write my first Class and its constructor function What is a Class? Let us see it this way: ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Object Oriented Programming - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  3 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 25-09-07, 07:06
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Tutorial 1 - my first AS 3.0 class

How to write my first Class and its constructor function

What is a Class?

Let us see it this way: think about the construction game LEGO and its colored blocks which put together can build wonderful construction
Here you are, a Actionscript 3.0 Class is a LEGO building block.
The Class can be used and called (instantiated) as wanted when and where we need it.
Once the instantiate of the Class is created, its methods and properties are available whenever we want them, but it is something I will explain in the next lesson.
For now, I would like to show you how to write a simple Class and tell you what is necessary to make it works.

First of all, with Actionscript 3.0, it is needed to implement/create a package. I find it useless explaining at this point in details what a package is as it would only confuse you even more. So let us simply say that a package is a box into which can contain more Classes. To this box, we also can assign a name. I will get into that later on.

Follow me...

For the time being, we implement a package without a name and accessible the following way:

I create an AS file and save it as 'Main.as' (PS: I recommend you to give it a name starting with a capital letter) and I write:
Code:
package
{
    
}
as Flash CS3 has a Class structure subdivided in packages, we will import the correct package of classes.
In this case, we will use that Class as a Document Class, meaning as the main class of our project or application. So we will import the MovieClip Class. (PS: who wants more info on the Document Class can read the article ėThe Document Class of Flash CS3ķ)
Why the MovieClip class?
Because, we need it to inherit its properties and its methods. It is still too premature to explain it in details so I invite to only keep in mind that the MovieClip Class needs to be imported.

To import the MovieClip Class, included in the package flash.display, we write:
Code:
package
{
    import flash.display.MovieClip;
}
with the import command, we tell Flash to import something which will then be available for us to use while developing our application. We then tell Flash that we want the MovieClip Class contained in the package flash.display.

We can now declare our class the following way:
Code:
package
{
    import flash.display.MovieClip;
    
    public class Main extends MovieClip
    {
        
    }
}
public means that the class is public. This attribute makes the class accessible even from outside the package. Meaning that we will be able to instantiate the class Main.as from another class or from the timeline.
class let Flash know that we are declaring a new class.
Main is the name of the class. The name of the .as file and the declaration of the class needs to be the same.
extends is an attribute which I would tell you to take it as a rule. If our class is to be the main class of our project (the Document Class), it needs to include the attribute extends. The class extends and inherits the methods and properties of the MovieClip Class. In the following lessons, you will understand the use of it better.
MovieClip simply means that our class extends the pre-existing class in Flash named MovieClip

Now, we need to define the building function (or constructor) of our class Main.

I will explain the concept of the building function:

The building function is the function which will carry out the code contained into it at the time the class Main is instantiated, so when we will instantiate Main from the timeline via the text field named Document Class in the Property Panel of our FLA.

The building function needs to:
- have the same name and case as the class
- do not need to be called. We simply need to create the instance of the class and Flash will automatically carry out the codes
- can be implemented and receive parameters (see next lesson)
- do need to be defined. If you do not do it, Flash will do it for you and so you will not be able to assign codes to carry out when the class is being instantiated
- can only be public, hence with the attribute public
- can not return a value

The building class of Main.as is defined the following way:
Code:
package
{
    import flash.display.MovieClip;
    
    public class Main extends MovieClip
    {
        public function Main()
        {
            
        }
    }
}
From now on, our class Main is ready to be used.

How to use it? Simple!

I create a main FLA and save it as ėmain.flaķ in the same folder as our Main.as.
In the option field named Document Class located in the Property Panel of the FLA, I write: Main (without the .as extension).

If we want instead to instantiate our class Main.as via coding, we would have to write:
var main:Main=new Main;
We will get later on into this concept. For the time being, let Flash do that job by simply inserting Main in the ready-made option.

We can now understand easily the point of the building function (public function Main). Back to the Main.as file, let us add a trace in the building function so to check that the trace is carried out as soon as the SWF is published:
Code:
package
{
    import flash.display.MovieClip;
    
    public class Main extends MovieClip
    {
        public function Main()
        {
            trace(" Main class has been instantiated correctly ");
        }
    }
}
we publish the SWF and if everything has been done correctly, Flash will immediately carry out the code contained in the building function and the result of the trace should then appear in the output window.

As you can see, even though the function Main has not been called directly, the code has been carried out. From the moment the class is being instantiated, the included code is being executed.

Source files:
Attached Files
File Type: zip tutorial_1.zip (6.1 KB, 97 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 05-06-08 at 13:42..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 27-10-07, 07:15
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Tutorial 1 - my first AS 3.0 class

Thank you to Abdulmohsen Amoudi for his translation.
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-12-08, 18:25
Flash Student
 
Join Date: Dec 2008
Location: Manhattan, New York City
Posts: 9
Rep Power: 0
xFlashStudentx is on a distinguished road
Re: Tutorial 1 - my first AS 3.0 class

I see. Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 1 07-01-09 18:54
tutorial 2 - le MovieClip Flep Flash CS3 base - tutorials 12 26-10-08 16:15
Tutorial 4 - the functions Flep Actionscript for beginners - tutorials 1 21-05-08 10:31
How to call another class from the Document Class with Flash CS3 Flep Tutorials 0 09-10-07 19:50
tutorial 2 - the properties of my class Flep Object Oriented Programming - tutorials 0 27-09-07 06:36


All times are GMT. The time now is 23:52.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap