A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: (MX) inheritance question

  1. #1
    Junior Member
    Join Date
    May 2003
    Location
    Spain
    Posts
    19

    (MX) inheritance question

    while supposedly it is possible to have a class inherit the methods and properties from other classes.
    For MovieClips this is only possible with an 'Object.registerClass ' (right? ) my question is: Is there an actionScript trick to make movieClips inherit from different classes, like registering selectively and therefore choose the methods / properties you want on each MC? i'd guess you have to combine the classes into Subclasses or something and then have MCs inherit from those... although you should be very aware of the possible combinations you may need...
    etc

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361

  3. #3
    Junior Member
    Join Date
    May 2003
    Location
    Spain
    Posts
    19
    oh thanks yes, I read that a few days back, but I can't get the fla you posted somehow...
    I must say I got a little confused at some points, English is not my native language so I would say I'm not really sure if that answers my question bwahaaha
    anyhow, I sorta know how to register classes to MCs, but my question was if it is possible to do it selectively from various classes for the same MC, like suppose this works :

    object.registerClass("myClip_1",Class1)
    object.registerClass("myClip_1",Class2)

    Perhaps what I try to accomplish is to do all this to [I]instances[I] instead of symbols in the Library... Also, you mention __proto__ thing which I really haven't used, could this be it?

    Thanks alot btw...

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    With object.registerClass you can set the same library instance to be associated with different classes, but you just have to create each instance of that library for each class during that association. Thats why object.registerClass works best with attachMovie :

    code:

    Object.registerClass("myClip_1",Class1);
    attachMovie("myClip_1", "instance1", 1);

    Object.registerClass("myClip_1",Class2);
    attachMovie("myClip_1", "instance2", 2);



    this makes that association with different clips of the same library item put on the screen at the same time from the timeline quite difficult with this method.

    Alternatively, as mentioned in the link, you can set inheritance with existing clips on the screen individually by setting the __proto__:

    code:

    my_mc.__proto__ = Class1.prototype;



    Then the my_mc movieclip will have access to all the methods in the Class1 prototype. The __constructor__ part was to run the class constructor for the movieclip. Now you dont have to set __constructor__ that way to run the constructor in the clip; instead you can just use something like:

    code:

    Class1.call(my_mc);



    Using __constructor__ is just similar to the inheritance method for classes using __proto__ which also sets __constructor. i.e.

    code:

    SuperClass = function(){}
    SubClass = function(){}

    SubClass.prototype.__proto__ = SuperClass.prototype;
    SubClass.prototype.__constructor__ = SuperClass;



    which serves as an alternative to

    code:

    SuperClass = function(){}
    SubClass = function(){}
    SubClass.prototype = new SuperClass();


  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    Hi senocular,

    I learn much from your post.

    Some tutorial says that Action script does not support "mulitple inheritance". Do you have any work-around or comments ?

    Thanks.

  6. #6
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    yeah, no multiple inheritance for Flash- at least not to my knowledge.

    Heres a work around:

    code:

    // for setting up another (2 total) superclass:
    addSecondSuper = function(subClass, superClass){
    subClass.prototype.__resolve = function(prop){
    return superClass.prototype[prop];
    }
    }

    // super 1
    ClassA = function(){}
    ClassA.prototype.methodA = function(){
    trace("Class A method");
    }

    // super 2
    ClassB = function(){}
    ClassB.prototype.methodB = function(){
    trace("Class B method");
    }

    // subclass
    ClassX = function(){}
    ClassX.prototype = new ClassA(); // normal inherit from A
    addSecondSuper(ClassX, ClassB); // set up class B

    instance = new ClassX();
    instance.methodA(); // traces "Class A method"
    instance.methodB(); // traces "Class B method"



    As long as the subclass or the 'real' inherited class (class A) doesnt have the method being called by the subclass instance, then the added second subclass method will be called (assuming it exists).

  7. #7
    Junior Member
    Join Date
    May 2003
    Location
    Spain
    Posts
    19
    Excellent!... thanks senocular, this was truly helpful... and ericlin, I didn't know how to say it in one sentence hahaha...

    the addSuperClass looks great, I've never seen the __resolve before, how it works though? Why isn't it:

    code:
    addSecondSuper = function(subClass, superClass){
    subClass.prototype=new superClass();
    }




    Also, what is the 'prop' argument?

    Sorry for the flood of questions, this will make a fabulous tutorial at the end

  8. #8
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    __resolve is a function (hidden feature, no assurance it will be around for future versions) that, when set, will capture all attempts to access non-existing properties or methods within an object. For example:

    code:

    // normal object definition
    myObject = new Object();
    myObject.propA = "A";
    myObject.propB = "B";

    // access properties (could also be methods)
    trace(myObject.propA); // traces "A"
    trace(myObject.propB); // traces "B"
    trace(myObject.propC); // traces undefined since C doesnt exist

    // set up resolve to recognize unknown properties
    myObject.__resolve = function(triedToAccess){
    // throw a trace up showing what was accessed and unknown
    trace("Tried to access "+triedToAccess+", but it doesnt exist");
    // whatever you return will be supposed value
    return this.propA;
    }

    // access properties (could also be methods)
    trace(myObject.propA); // traces "A"
    trace(myObject.propB); // traces "B"
    trace(myObject.propC); // traces:
    // Tried to access propC, but it doesnt exist
    // A



    Now, addSecondSuper is a workaround to the fact that Flash doesnt support multiple inheritance. Multiple inheritance is the ability for a class to inherit from multiple super classes which they themselves have nothing to do with (or inherit from) each other. See, normally, if you wanted some ClassX to inherit from ClassA and ClassB, then first either ClassA or ClassB would have to inhert from the other ie:

    code:

    ClassA = function(){}

    ClassB = function(){}
    ClassB.prototype = new ClassA(); // B now inherits from A

    ClassX = function(){}
    ClassX.prototype = new ClassB(); // X inherits from B which inherits from A



    But what if you dont want instances of ClassB to inherit from ClassA and you want them to be completely seperate but still have ClassX have access to both? Technically you cant. addSecondSuper, though, creates a workaround using __resolve to say, ok, if some instance of ClassX inherits from ClassA, but 'thinks' it also inherits from ClassB and tries to use an unknown method that is within the ClassB prototype but not its own prototype or that which it inherits from, then we can check for that unknown call using __resolve (which will be called when the unknown method is attempted to be used) and, if present in the ClassB prototype, return that so it can be run for our instance.

    Again, this is a workaround. subClass.prototype=new superClass(); is the basic way of handling inheritance and only works for one superclass. If you said something like:

    code:

    ClassA = function(){}

    ClassB = function(){}

    ClassX = function(){}
    ClassX.prototype = new ClassA();
    ClassX.prototype = new ClassB();



    Then ClassX will only inherit from ClassB as setting ClassX.prototype = new ClassB(); completely clears the prototype of what it was before, that being a ClassA instance which let it inherit from ClassA.

    Using __resolve, you can access that other superClasses methods if they are attempted to be used and dont exist within the current class or its current 'real' super.

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