Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 7 of 7

Thread: Tutorial 1 - my first AS 3.0 class

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Tutorial 1 - my first AS 3.0 class

    flash templates
    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

  2. #2
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Re: Tutorial 1 - my first AS 3.0 class

    Thank you to Abdulmohsen Amoudi for his translation.

  3. #3
    Flash Student Settled In xFlashStudentx is on a distinguished road
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Re: Tutorial 1 - my first AS 3.0 class

    I see. Thank you.

  4. #4
    Junior Member Settled In goki is on a distinguished road
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Re: Tutorial 1 - my first AS 3.0 class

    Thank you

  5. #5
    Junior Member Settled In plashmaddy is on a distinguished road
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Re: Tutorial 1 - my first AS 3.0 class

    Thank you

  6. #6
    Junior Member Settled In Antheor is on a distinguished road
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Re: Tutorial 1 - my first AS 3.0 class

    Thank you so much for your tutorials trying to explain the hardest thing : how it works !!! (not just as3 example)...

    Regarding this page info : you say document class can communicate with mc on stage. OK. But is it possible to communicate with those mc with non doc classes ?
    In the same scope :
    1. How can I communicate with graphic symbol in doc class (flash cs4 gives no 'name for as3 export' option for graphics) ?
    2. Iif I create a graphics in a class -in document class for example- how can I make this graphic reachable in other classes.

    OK, that makes 3 questions. It's probably a lot to answer ...
    Anyway, thank you so much for the time you spent to write the tutos :)

  7. #7
    Junior Member Settled In ksyz is on a distinguished road
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Re: Tutorial 1 - my first AS 3.0 class

    thanks

+ Reply to Thread

Similar Threads

  1. tutorial 2 - the properties of my class
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 3
    Last Post: 06-04-10, 08:32
  2. Tutorial 2 - gli Array
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 39
    Last Post: 23-02-10, 11:17
  3. Tutorial 3 - the methods
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 9
    Last Post: 07-05-09, 13:09
  4. Tutorial 1 - le variabili
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 19
    Last Post: 23-04-09, 13:08
  5. Replies: 0
    Last Post: 09-10-07, 18:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts