Posted in as2 by nicoB on June 28th, 2007

It’s been online for a long time now, but I’ve discovered it not so long ago. So there is a little tip that can be useful (for the ones who already know it, I’m sorry).

Sometimes, you have a class that extends a MovieClip which is empty. If you wan’t to attach this mc on the scene, you have to create a new empty symbol in the library and give him a linkage (identifier and class). It’s quite boring to do.

There is a solution to bypass this (found by Peter Hall) :

1. Create your class which extends MovieClip :

class net.nicoB.LinkedClass extends MovieClip {
     public function LinkedClass(){
          trace("hello");
     }
}

2. Insert this to create a linkage identifier:

public static var id:String = "__Packages.net.nicoB.LinkedClass";
private static var _isSymbolLinked:Boolean =
               Object.registerClass(id, LinkedClass);

the id must be “__Packages”+full package name

3. Attach it as you would if it was in the library, by specifying the static property id:

import net.nicoB.LinkedClass;
var mcLinked : LinkedClass =
              this.attachMovie(LinkedClass.id, "mcLinked", 1);

That’s it. It’s easy, quick and useful!

List of replies :

  1. ekameleon, says

    Hello

    it’s really not the better solution

    The best solution is to use the __proto__ key word :

    [code]

    var mc:MovieClip = createEmptyMovieClip(”myClip”, 1 ) ;
    mc.__proto__ = net.nicoB.LinkedClass.prototype ; // change the inherit
    net.nicoB.LinkedClass.call(mc) ; // launch the constructor

    [/code]

    or with an attachMovie :
    [code]
    var mc:MovieClip = attachMovie(”mcLinked”, “myClip”, 1 ) ;
    mc.__proto__ = net.nicoB.LinkedClass.prototype ; // change the inherit
    net.nicoB.LinkedClass.call(mc) ; // launch the constructor
    [/code]

    In my framework i use a class to encapsulate this features :
    http://svn1.cvsdude.com/osflash/vegas/AS2/trunk/src/vegas/util/factory/DisplayFactory.as

    EKA+



  2. nicoB, says

    Thank you for the tip EKA.
    Both methods are equivalent and lead to the same result.



  3. cddin, says

    “Both methods are equivalent and lead to the same result”

    but the thing is which one is the best practise?

    I agree with eka.. I belive that there are reason macromedia(now adobe) provide __proto__ key word –> it is to solve this situation

    just my 2 cents


Leave a Reply