A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: the logic of class

  1. #1
    Senior Member
    Join Date
    May 2006
    Posts
    145

    the logic of class

    I just realized why I came up with so much problem in learning as3. I just understood that I had not completely understand the concept of class system.

    now i know that for easiness, everything has a class. a movie clip class would look like this:
    Code:
    package {
    	import flash.display.MovieClip;
    	public class MOVIECLIP extends MovieClip {
    		var ALL_COMMON_VARIAbLES;
    		function ALL_COMMON_FUNCTIONS() {
    		}
    	}
    }
    }
    I also happened to know that a document class would look something very similar to movieclip class because it is also a subclass of flash.display.MovieClip.

    If something is wrong with my understanding, please tell me.

    What is incomplete from my understanding is where should I put my:
    Code:
    EVENT_CALLER.addEventListener(THE_EVENT,FUNCTION_CALLED)
    I know that the FUNCTION_CALLED could be MOVIECLIP.ALL_COMMON_FUNCTION
    .and sometimes I want my EVENT_CALLER to be stage, however stage, root, this, parent syntax cannot be used under package.

    specific examples are:
    1. code which i wish to be performed when the an instance of a movieclip is created. which previously is under OnClipEvent(load) written under the MovieClip in AS2
    2. an enterframe event which should not belong under any movieclip.

    so far my solution is to make a new movieclip called controller. but I know that it is a bad practice. so I am here in the rough way of trial and error to learn for a better coding logic.

    thank you in advance. and learning AS3 is like learning a whole new language.

  2. #2
    Senior Member
    Join Date
    May 2006
    Posts
    145
    thanks to :cancerinform
    I now can now make my class understanding better.

    Code:
    package {
    	import flash.display.MovieClip;
    	public class MOVIECLIP extends MovieClip {
    		var ALL_COMMON_VARIABLES;
    		function MOVIECLIP (){ //have to have the same name as the class
    		//all the code that will run by default once the class is run or loaded
    		}
    		function ALL_COMMON_FUNCTIONS() {
    		}
    	}
    }
    }
    if there are anything still missing, please tell me.

    Now a new problem is how to declare a global variable. I cannot somehow declare a global variable at the document class and call it or change it from the movieclip class. thank you.
    Last edited by ArielGenesis; 09-01-2007 at 12:36 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    6
    Hi!

    Just to share my important milestones to understand how to write and use classes combined with timeline code. It might help you! I'm still a beginner!

    1. It is not easy to understand it all starting from zero flash experience. You find much good help on the net, but starting from zero and get the full picture takes some time.

    2. If you don't create a class file flash does this for you in the background. So to get full control you can always create a classfile with the same name as your movieclip (with the suffix .as). Put it in the same folder as your .fla project file. Then rightclick on the moveclip and select linkage and type your classfile name there.
    In the constructor you can do things once at startup! I also have a _doOnce variable that I can use in my movieclip frames (if needed). You reach methods in other movieclip (classes) just by giving each movieclip an instance name (using the property tab). Here is a simple example. Here you have a movieclip called MyMovieclip and a classfile called MyMovieclip.as.

    Code:
    package {
      import flash.display.MovieClip;
      public class MyMovieclip extends MovieClip {
        // State variables
        private var _doOnce:Boolean;
    
        // Constructor
        public function MyMovieclip () {
           _doOnce = true;
           ...
        }
    
        ...
      }
    }
    3. You also need to have the main class of your flash application. That is the class that defines the overall behaviour. It might call methods in your moveieclips that your application consists of. This is a good place to have all your events for the user. Create a class file with the same name as your .fla file (in the same folder). Then type in the classname in the properties tab of your scene (Document class property). See this small example...


    Code:
    package {
      import flash.display.MovieClip;
      import flash.events.MouseEvent;
      import flash.events.KeyboardEvent;
      import flash.ui.Keyboard;
      public class MySuperGame extends MovieClip {
        // State variables
        private var _doOnce:Boolean;
    
        // Constructor
        public function MySuperGame() {
           _doOnce = true;
           // Add event listners
           stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
        }
    
        // On any key down
        private function reportKeyDown(event:KeyboardEvent) {
          ...
          myMovieclip.MyMethod(...);
        }
      }
    
      ...
    }
    4. The build in help is not very helpful. You don't get any help when writing the code (like get a list of all avalibe methods when typing a . after the instance variable). So to have a book in ActionScript 3 is very useful. Like "ActionScript Cookbook O´REILLY". The net also gives some help...

    5. The code that you place in the movieclip frames is somehow included in your class. I see the timeline frame code as a method in the class. How this is done is still a bit magic for me. A good rule is to keep the timeline code to a minimum.

  4. #4
    Senior Member
    Join Date
    May 2006
    Posts
    145
    Wow, that's something. That's what I ve been searching for the whole net! THANK YOU.

    I got question sir! sometimes we use private sometimes we use public sometimes we put nothing at all in front of a function. I know what those words means, however, so far I haven't find a case where any of those matter. if I change a private function into public, does it really matter?

    and...
    You reach methods in other movieclip (classes) just by giving each movieclip an instance name (using the property tab).
    how if my movie clip does not have an instance in the stage. it is just in the library. i.e I have this code under a movieclip class, in a function of course:
    Code:
    y2=(root.mouseY-y)/700+gravitasi;
    however, gravitasi (no typo hehehe) is under the document class. what I tried was:
    Code:
    var WHERE_GRAVITASI_IS:DOCUMENT_CLASS = new DOCUMENT_CLASS();
    trace(WHERE_GRAVITASI_IS.gravitasi
    which return in an error:

    Error #2136: The SWF file file:///D|/flash/lalat/DOCUMENT_CLASS.swf contains invalid data.


    thankssssssssss
    Last edited by ArielGenesis; 09-01-2007 at 09:34 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2007
    Posts
    6
    First of all get a book and read it - all will fall in place then!

    Sometimes we use private sometimes we use public sometimes we put nothing at all in front of a function. I know what those words means, however, so far I haven't find a case where any of those matter. if I change a private function into public, does it really matter?
    You use private to hide a method from other classes. A private method can only be used inside it's own class. To hide methods, variables a s o is a good practice, you prevent other classes to reach internal stuff by misstake. And you are right in that it does not matter if you change all your private methods to public - everything works the same but you will expose things that don't need to be exposed. A constructor allways needs to be public. Otherwise no one (no other class) would be able to create a new instance of this class - and the class could not be used...

    How if my movie clip does not have an instance in the stage. it is just in the library. i.e I have this code under a movieclip class, in a function of course
    All stuff in the library is classes not instances (no one has yet called the class constructor to create an instance). If you don't give a movieclip on the stage an instance name you can not call it's (public) methods and variables from any other class. A soon as you start your flash application all constructors will be called starting from the top level (your "main" class with the same name as your .fla file). You can add a trace("My ... constructor is called"); in all of your class constructors to see the order that all your class instances are created when you start the flash application.

    You can also create classes that don't have any movieclip. It can be useful things that you like to use in several of your other classes. This is a small example:

    Put this in the same folder as your .fla file and name the file Counter.as

    Code:
    package utilitiesClasses {
       public class Counter {
          // State variables
          // Note! It's good to keep this private.
          // Only via the public methods this variable can be changed.
          private var _count:int;
    		
          // Constructor
          public function Counter() {
            _count = 0;
          }
    
          // Reset
          public function reset() {
    	_count = 0;
          }
    
          // Get next
          // Note! public is needed.
          // Otherwise you can't call this method from outside this class
          public function getNext():int {
            return _count;
          }
        }
    }
    To use this in some other class (just some dummy example...):

    Code:
    package {
      import flash.display.MovieClip;
      import utilitiesClasses.Counter;
      public class MyMovieclip extends MovieClip {
        // State variables
        private var _doOnce:Boolean;
    
        // Constructor
        public function MyMovieclip () {
           _doOnce = true;
           _myCounter = new Counter();
           ...
        }
    
        public function test() {
           trace("Counter=" + _myCounter.getNext());
        }
        ...
      }
    }
    Last edited by - Zapp -; 09-02-2007 at 08:30 AM.

  6. #6
    Senior Member
    Join Date
    May 2006
    Posts
    145
    humm smart. I might use a class, just a class to store all my constant or shared variables... or shared functions. sorry sir, but i dont think i am buying any books. simply i dont think it is worthed for something like my mere seasonal hobby. thankyou very much, I just have a great brain leap!

  7. #7
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Just a few notes, there are many good reasons for making a class constructor private, the most important being the singleton pattern:
    http://en.wikipedia.org/wiki/Singleton_pattern

    Speaking of patterns, reading about various design patterns may or may not help you understand how classes can be used. I personally find design patterns rather confusing if you don't understand oop in the first place, but they are worth a shot:
    http://en.wikipedia.org/wiki/Object-...ed_programming
    http://en.wikipedia.org/wiki/Design_...ter_science%29
    There is a list of patterns on the design pattern page, I suggest at least going through some of the more common ones and see if anything clicks:
    Model-View-Controller
    Singleton
    State
    Strategy

    Wikipedia is arguably written by nerds, who probably have a pretty good idea of what they are doing, browsing around the oop related articles would be a good idea.
    Last edited by 691175002; 09-03-2007 at 11:39 PM.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  8. #8
    Senior Member
    Join Date
    May 2006
    Posts
    145
    Wikipedia is arguably written by nerds
    I have to agree, it would be my quote of the week!

    I only read the OOP link, I tried to comprehend the rest, but I failed...

    Code:
    _myCounter = new Counter();
    when I tried to used this code. it works! however when i used it to store a value, I cannot call the same value from another object outside of the class, which is what I want.

  9. #9
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Too bad constructors can't be private in Actionscript. Maybe you should learn what you are talking about before you go telling other people facts that are not true and confuse them even more.

    (I am referring to 691175002)

  10. #10
    Junior Member
    Join Date
    Aug 2007
    Posts
    6
    Back to ArielGenesis problem _myCounter = new Counter();... that you want to be accessable from several classes (instances of different classes). This referes to some code earlier in this thread...

    To do this the simplest way is to use a static variable (and maybe static methods to get and set the value). A static variable or method is avalible direct in the class - you don't need to instanciate the class to use it! And it is shared among all instances of the class.

    So if you have a static variable you simply access with the class name like Counter._myCounter (if it is a public variable - bad practice). And if you have a static method you simply do int c = Counter.getNext() from any other class.

    By the way, classes always starts with capital letters like Counter. General variables in methods with small letters. And class variables with underscore like _myCounter. Methods starts with small letter likegetNext().

    Example
    Code:
    package utilitiesClasses {
       public class Counter {
          // Static class state variable
          private static var _count:int;
    		
          // Constructor
          public function Counter() {
            // Does nothing useful in this case
          }
    
          // Reset class method
          public static function reset() {
    	_count = 0;
          }
    
          // Get next class method
          public static function getNext():int {
            return _count++;
          }
        }
    }
    Please note that I'm still myself a beginner - so there might be smaller things that are not 100%, but I also think that in this beginner thread there is no need to talk about designpatterns a s o - it's only confusing.

  11. #11
    Senior Member
    Join Date
    May 2006
    Posts
    145
    ooh, so the best think I can do is not:

    Code:
    //under some other classes in some function
    var counter:int = Counter._myCounter
    but rather
    Code:
    //under some other classes in some function
    var counter:int = Counter.getCountNow()

    where
    Code:
    package utilitiesClasses {
       public class Counter {
          // Static class state variable
          private static var _count:int;
    		
          // Constructor
          public function Counter() {
            // Does nothing useful in this case
          }
    
          // Reset class method
          public static function reset() {
    	_count = 0;
          }
    
          // Get next class method
          public static function getNext():int {
            return _count++;
          }
    
    // Get count now class method
          public static function getNext():int {
            return _count;
          }
    
        }
    }
    and I'll write
    Code:
    import utilitiesClasses .Counter
    at the beginning of classes that want to use access counter.
    so basically, var counter:int = Counter._myCounter, will never work?

  12. #12
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    It is generally a good idea to use getter and setter methods to access properties.

    There is no reason for a property to be public, and having a getter and setter function means that you can filter the data and make sure that it doesn't screw up the class.

    The goal when designing any class should be to make it as bulletproof as possible, that means little or no dependencies and only allowing access where you want to.

    The problem with public properties is that you cannot prevent them from being changed. Its pretty much a non-issue for something this simple, but if some smart guy decides to change some internal property that is only supposed to be read, if that property is not protected you are in for a world of strange bugs and unpredictable behavior.

    A class should be able to go through hell and back and still be in a usable state.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  13. #13
    Senior Member
    Join Date
    May 2006
    Posts
    145
    very nicely put, thank you.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center