WTF is createClassObject, createObject, and destroyObject?

Lemme get that for you. Typically the documentation explains these as “object creators” which is kind of… well, generic. They do more (or less) than that, and they are a lot more approachable than the docs lead on. They are, however, integral that you learn when you mess with the framework, for if nothing else, to kick it good form style. I don’t know everything, but here’s what I do know.

createObject

Replacement for attachMovie in the Flash MX 2004 component framework. A wrapper function for attachMovie, it operates the exact same. It returns whatever attachMovie returns; typically the movie clip/component object your attaching. You pass in the same parameters.

destroyObject

Replacement and extension for removeMovieClip in the Flash MX 2004 component framework. A wrapper function for removeMovieClip, it extends it to support authortime clips as well. Since authortime components (objects manually dropped on the stage at authortime) are automatically compiled to reside in negative depths, removeMovieClip will not work on them. This method, if it detects an authortime component trying to be removed, will find a depth that is unoccupied and empty, bring the component to that depth, and remove it. Finally, it deletes a reference to the movie clip’s name (no clue why since you just removed it).

The difference with this method vs. removeMovieClip however is that it takes a string parameter; the movie clip/component’s name as a string. Therefore, instead of doing:

my_lb.removeMovieClip();
// or
removeMovieClip(my_lb);

You’d do:

destroyObject(my_lb._name);

The framework itself uses this method because there are a ton of string references to objects in the other classes because of skinning, for example. Still, that’s not enough for me to understand why they did it this way. Maybe Java’s Swing does this… :: shrugs ::

createClassObject

An replacement and extension of attachMovie in the Flash MX 2004 framework. Typically, you use similiar to attachMovie, except you use your class name. For example:

var ref_lb = createClassObject(mx.controls.List, "names_lb", 0);

The first parameter, however, determines how the function works. Most classes created to work in the Flash MX 2004 framework will have a 2 static variables defined called symbolOwner and symbolName. The symbolName one represents a string pointing to the component’s linkageID. The symbolOwner is a reference to the class’s class name, or package path. In List’s case, that is mx.controls.List; the object on global that holds his class definition.

This function assumes if the class you pass in doesn’t have a symbolName, then it must be a sub class. Since this function calls createObject, and thus attachMovie, your creating a movie clip and getting a reference to it. The way Flash 6 & 7 associate class methods with a movie clip is via the method Object.registerClass. This takes the movie clip’s symbol linkage name (got that?), the class, and basically says, “Yo, if you ever get attached, you’ll now have all the powers of this class; properties, methods, you name it.” This can get weird if your class doesn’t inherit from MovieClip somewhere up the chain… at least in wrapping your head around the concept, anyway.

Therefore, if your class is a sub class, it’ll register it via Object.registerClass with the class’ symbolName/linkageID. After attaching the component/movie clip via createObject, assuming you passed in a sub-class, it does the registering again with a slight difference; it uses the className’s symbolOwner/class name instead of just the className you passed in. Not sure of the significance of the difference.

Conclusion

These are support methods, really, for a lot of the framework. Additionally, you can now see where the symbolName and symbolOwner static variables you may have been defining in your components are used. If these methods don’t work, it’s probably because those variables are not defined or miss-spelled. I’ve copied and pasted classes before to save time in rewriting a new class. I sometimes forget to change these variables with the class name.

12 Replies to “WTF is createClassObject, createObject, and destroyObject?”

  1. How can I dynamically create the class name in a createClassObject call? In my app I will never know what my components are called becuase they are controlled by a content management system. I will be getting the names of the classes dynamically from the CMS via Web Services in an array of objects, so how could I get something like the example below to work:

    //kind of how it’s passed from the Web Service
    var HM1:Object = new Object();
    var tmpArray = new Array();
    tmpArray[0] = HM1;

    //needs to work something like this
    createClassObject(tmpArray[0], variable, 0);

    //or I might even have the component name in a string and could use something like this if it worked

    createClassObject(Object(tmpObjName), variable, 0);

    has anyone tried this? or have any ideas how to accomplish this?

    thanks.

  2. If your class is already defined via import, they reside on global. Therefore, you can do:

    createClassObject(_global[HTM], ‘mc’, 0);

    However, if your receiving a class back from a webservice, and it’s not already defined in your SWF, this won’t work.

  3. ok, that sounds interesting, but it doesn’t seem to be working. i tired importing the whole package like:
    import com.thISit.components.pageGroupTypes.*;
    and individually like:
    import com.thISit.components.pageGroupTypes.HM1;

    here is what this part of my component looks like:
    import com.thISit.components.pageTypes.*;
    class com.thISit.components.PageGroupTypeContainer extends mx.core.UIObject {
    static var symbolName:String = ‘com.thISit.components.PageGroupTypeContainer’;
    static var symbolOwner:Object = Object(com.thISit.components.PageGroupTypeContainer);
    var className:String = ‘PageGroupTypeContainer’;

    public function loadComponent(pagecode:String):Void {
    createClassObject(_global[pagecode], ‘mc’, 0);
    }
    }

    thanks for your help. there doesn’t seem to be a lot of info on this stuff. also this is used in Flex, not sure if that makes a difference.

  4. Interesting… Those classes your definining, see if you can see them. Debug your movie and look on _global. If you don’t see the classes your importing, then that’s why. If you do or do not see them, let me know.

  5. Strike that, I do now. Page 38 of the Flex Builder Tutorials.pdf shows you how. You basically control click a breakpoint next to the ActionScript line in question, and then click the debug button instead of run on the MXML file running it; you’ll then switch to the AS file when that part of AS is run.

  6. Say I have a declared class List with

    var className:String = ‘List’;
    static var symbolOwner:Object = one.two.List;

    ////////////////////////////////////////
    At runtime I want to to access the path of the class, eg ‘one.two.List’, of any object I instantiated.

    problem 1) List.symbolOwner returns Function type.
    problem 2) [ instanceObj.className ]symbolOwner is not recognize.

    If you have any suggestions, thanks aheaed.

Comments are closed.