A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [OOP]constructor function

Hybrid View

  1. #1
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973

    [OOP]constructor function

    I am trying to learn oop and i am confusing about how to build a constructor function.I mean, i dont understand scopes, what should i put in or out, or even IF i should put something in...
    I mean, if i dont have any argument to the new class, so i dont need to put anything inside the constructor function, isnt it?
    Thanks in advance

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    dont overcomplicate things. don't code to code - think about what you want to achieve, and then decide how best to approach it. it's very important to remember that classes and protos and extensions and what not are in all cases just aping core functionality - generic prepackaged representations of what somebody else thinks you want - there's not a single method or capacity available to a class member that isn't already available through the core (more efficient, more manageable, faster) routes. and most importantly, ignore the trends, figure it out on your own, and disregard the oop-la (ha i just made that up!).

  3. #3
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    The constructor is used to initialise the the class object, so you can set up a load of initial properties/methods/events etc. If you don't need to do that then you don't need to include a constructor function at all (there is not much point having an empty function).

    For example, sometimes you'll want to write a class full of static functions that is just a load of funtionality that does not rely on any internal class properties, so you wouldn't need a constructor for that.
    Code:
    // example of a static class with no need for a constructor function
    class PointlessMathClass
    {
        public static function addthese(a:Number, b:Number):Number
        {
            return a+b;
        }
        public static function multiplythese(a:Number, b:Number):Number
        {
            return a*b;
        }
    }
    Or perhaps you never need to set specific properties for a class, they will always be the same for each class object. You might not need a constructor then either.
    Usually you need to initialise something though, so most classes do tend to have a constructor function.

    For example you often want to pass specific property values to the class object when you create it.
    Code:
    // a useless class that does nothing but store a point...
    class PinPoint
    {
        // x/y properties of the class object
        private var __x:Number;
        private var __y:Number;
    
        // constructor
        function PinPoint(x:Number, y:Number)
        {
            // initialise the internal x/y properties
            __x = x;
            __y = y;
        }
        
        // getters/setters
    
        public function get _x():Number
        {
            return __x;
        }
        public function set _x(n:Number):Void
        {
            __x = n;
        }
        public function get _y():Number
        {
            return __y;
        }
        public function set _y(n:Number):Void
        {
            __y = n;
        }
    }
    As for scope; any vars declared within a function have the scope of that function only and are deleted once the function has finished running. Any vars declared inside the class but outside any functions have the scope of the class object and can be accessed by the functions within the class (or accessed from outside the class, as properties of the class object, if they are public).
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  4. #4
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    moagrius:my goal is to create a vector class based in tonypa's vectors, http://www.tonypa.pri.ee/vectors/index.html
    so i can apply vectors to movieClips like this:
    vik = new vector();
    vik.applyTo(MyMovieClip);
    And them add more and more vectors, so i can -in a easy way- simulate phisics stuff

    Lexicon, thanks for you reply, what i am confused is this:
    Whats the diference between : i define a propertie inside a constructor
    or
    define a propertie outside the constructor
    I mean, whats the diference for the instance
    i mean, if it has something to do to with all the instance of the class changing the same propertie like this:
    instA.setMyProp += 1;
    trace (instA.getMyProp);//traces 1

    instB.setMyProp += 1;
    trace(instB.getMyProp);//traces 2

    OR (and thats the way i want)each instance has his own myProp, this way:
    instA.setMyProp += 1;
    trace (instA.getMyProp);//traces 1

    instB.setMyProp += 1;
    trace(instB.getMyProp);//traces 1
    I dont know if i make myself clear...

  5. #5
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    not really sure what you are asking.

    The constructor is there to initialise an instance of the class, that might be to set up a few initial variables, set up some events, register itself with some other class, or anything else that you want to initialise when the class object is created.

    You are describing getter/setter functions or changing public variables, which are basically ways of changing the properties of the class after it has already been created.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  6. #6
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    SO, when i need the constructor function just to put values inside the properties of the instance, i dont even need a constructor
    function at all, cos i can use functions inside the as like:
    function setAge(cag:Number){
    age = cag;
    }
    instead of define age inside the constructos
    ------
    It seems that whats inside the as but outside the constructor belong to the instnce, to create what belong to the class must use this static thing

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Yes, but you often initialise a class instance with properties.

    And it's more convenient to write...

    Code:
    var coords:PinPoint = new PinPoint(10,20);
    instead of

    Code:
    var coords:PinPoint = new PinPoint();
    coords._x = 10;
    coords._y = 20;
    when creating an instance of the class in the swf.
    Last edited by Lexicon; 02-01-2006 at 11:43 AM.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  8. #8
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    Thanks again!!!
    Now...how do i make a class to do something every onEnterFrame?

  9. #9
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    An onEnterFrame event has to be assigned to a MovieClip. So you could pass a reference of a movieClip into the class somehow and have it assign a function to it.

    Code:
    // timeline code
    var yak:Repeater = new Repeater();
    yak.startEvent(someMovieClip);
    
    
    // class code
    class Repeater
    {
        function myOnEnterFrame(Void):Void
        {
            trace("hello");
        }
        
        function startEvent(mc:MovieClip):Void
        {
            mc.onEnterFrame = myOnEnterFrame;
        }
    }

    Or you can extend the MovieClip class and 'link' a MovieClip to that class. The simplest way to link a class is to right click on a movieClip in the library and export for actionscript, you can fill in the class there.

    Code:
    class Repeater extends MovieClip
    {
        function Repeater()
        {
            this.onEnterFrame = myOnEnterFrame;
        }
    
        function myOnEnterFrame(Void):Void
        {
            trace("hello");
        }
    }
    or
    Code:
    class Repeater extends MovieClip
    {
        function onEnterFrame(Void):Void
        {
            trace("hello");
        }
    }
    I can see already this is going to lead on to a million questions because a can of worms has been opened

    I think you really need to read some tutorials because there is quite a lot of different things to explain with classes and OOP in general...

    http://www.kirupa.com/developer/oop2/AS2OOPindex.htm
    http://flashscript.biz/MX2004/OOP_tutorial/lesson1.html

    Those links should help.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  10. #10
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    Now i am really confused to death
    Tryed to use this Repeater class (the second one, seems simpler)and dont seems to work
    In jhg.fla i go:
    ac=new Repeater;
    ac._x = 10;
    ac._y = 20;
    i am using MX 2004

  11. #11
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    This is why you need to read the tutorials I posted links to.

    You are trying to run before you can walk.
    The examples that extend the MovieClip are not declared the same way as you might other classes as they need to be linked to a MovieClip. This is done by right clicking on a movieClip symbol in the library, selecting 'linkage' and filling in the appropriate details.
    Attached Files Attached Files
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  12. #12
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    THAnks Lexicon!!!
    Now i see, its working beautiful!!
    I didnt fully understand examples 1 and 2, but example 3 yes i did!
    I will ask things about the toturials later, maybe open another thread

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