A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Register an already present object to a constructor

  1. #1
    Senior Member
    Join Date
    Jul 2000
    Posts
    334

    Register an already present object to a constructor

    I am trying to create constructor object called "moveableObj".
    I seem to have trouble figuring out how to use it or access it though.
    Here is my code:
    Code:
    //create the constructor
    moveableObj = function(ref){
       //give it some property
       this.test = "test";
    }
    
    //create a method()
    moveableObj.prototype.moveBounce = function(args){
    code here....
    }
    
    //attempt to register the class to an already present item on the stage with
    //an instance name of mc_obj
    Object.registerClass("_root.mc_obj", "moveableObj");
    
    //attempt to access the method
    mc_obj.moveBounce ();
    //attempt to trace a property
    trace(mc_obj.test);
    There is obviously some problems in there.
    1) If I have an already present MovieClip on the stage and want to assign it to the moveableObj constructor - how do I do that - I thought Object.registerClass but that seems to be if you have an item in the Library and are using Linkage - I am not.

    2) Once I have the already present MC assigned to the moveableObj construstor - how can I call a method that will affect the assigned object?
    e.g. mc_obj.moveBounce ();

    Any help GREATLY appreciated - thanks!

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    yeah, registerClass works on clips with Linkage IDs only, registering that class to be associated with all instances of that library item with that linkage ID.

    Otherwise, you will have to manually run the constuctor function through that movieclip to declare/set those properties and would either have to instead use movieclip.prototypes for the methods or throw them into the movieclip instance - either individually or through redirecting the __proto__ of that clip.

    Code:
    //create the constructor
    moveableObj = function(ref){
       this.ref = ref;
       this.test = "test";
    }
    
    //create a method()
    moveableObj.prototype.moveBounce = function(args){
    	trace("bounce " + args);
    }
    
    // mc_obj.__proto__ points to the movieclip prototype
    // changing it to a new moveableObj will let it inherit
    // from the moveableObj class, though we lose the movieclip
    // references
    mc_obj.__proto__ = new moveableObj("my ref");
    // since mc_obj.__proto__ is now a new moveableObj, 
    // mc_obj.__proto__.__proto__ references the moveableObj
    // prototype.  moveableObjs also inherit from the generic
    // object Object in Flash which is the next in line of the
    // __proto__s.  So what we can do is change that to a movieclip
    // which itself inherits from the object Object but also
    // brings back the movieclip inheritance we lost from the
    // previous line of code
    mc_obj.__proto__.__proto__.__proto__ = new MovieClip();
    
    // attempt to access the method
    trace(mc_obj.moveBounce);
    // test method
    mc_obj.moveBounce(10);
    // attempt to trace a property
    trace(mc_obj.test);
    // attempt to trace another property
    trace(mc_obj.ref);
    // check existing movieclip function for existence
    trace(mc_obj.getDepth);
    // test method
    trace(mc_obj.getDepth());
    
    /* output ***
    [type Function]
    bounce 10
    test
    my ref
    [type Function]
    -16383
    ************/
    now if you just want to run the constructor in the movieclip, then you can have the constructor to be assigned a valid function of the movieclip or its prototype and it will be run in its scope.

    Code:
    mc_obj.const = moveableObj
    mc_obj.const("my ref");
    // attempt to trace a property
    trace(mc_obj.test);
    // attempt to trace another property
    trace(mc_obj.ref);
    
    /* output ***
    test
    my ref
    ************/
    here however, you dont get the method inheritence. Alternatively, though, you could throw the moveableObj methods in the movieclip prototype at this point

    Code:
    // ...
    MovieClip.prototype.__proto__ = moveableObj.prototype;
    // attempt to access the method
    trace(mc_obj.moveBounce);
    // test method
    mc_obj.moveBounce(10);
    
    /* output ***
    [type Function]
    bounce 10
    ************/
    Though at that point, all movieclips will have access to that method as well. If you dont want that its a matter or copying them over into the instance itself

    Code:
    // or...
    for (method in moveableObj.prototype) mc_obj[method] = moveableObj.prototype[method]
    // attempt to access the method
    trace(mc_obj.moveBounce);
    // test method
    mc_obj.moveBounce(10);
    
    /* output ***
    [type Function]
    bounce 10
    ************/
    So theres a few takes on it depending how involved you want to get.

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