Dynamic MovieClip Registration with AS2

| 17 Comments

This handy little class (download here) will allow you to change the registration point of a MovieClip on the fly (instead of having to modify the symbol in the library and repostion all of the contents).

Usage: For every MovieClip you want to apply this to, navigate to it's Linkage properties in the Library, make sure it is exported for ActionScript, change the AS 2.0 Class to "com.darronschall.DynamicRegistration" and then use it in script like this... (also make sure that the DynamicRegistration class is in your movie's classpath)

UPDATE 7/28/05 - I goofed when I originally made this code. It's a bad idea to have to extend this class, rather it works much better with a mixin approach. So, I've updated the zip file, but usage is a lot easier. Rather than dealing with the linkage and stuff, simply call: com.darronschall.DynamicRegistration.initialize(my_clip); and my_clip will be decorated with setRegistration, _x2, _y2, _xscale2, _yscale2, _xmouse2, _ymouse2, and _rotation2. This is much easier to use... sorry about not fixing this for 2 years.. :-P

import com.darronschall.DynamicRegistration; // Assume there is an instance named square_mc on the stage var square_mc:MovieClip; // updated 7/28/05 - add the dynamic registration stuff at runtime to our square movieclip DynamicRegistration.initialize(square_mc); // The square_mc has an original registration at 0,0 so // let's change that to 10, 60 at runtime. square_mc.setRegistration(10, 60); // Now whenever we access a property of the square_mc that deals // with the registration point, use a "2" after the property name... // These are the available properties: square_mc._x2 = 4; square_mc._y2 = 7; square_mc._rotation2 = 40; square_mc._xscale2 = 140; square_mc._yscale2 = 80; // square_mc._xmouse2 is readonly // square_mc._ymouse2 is readonly

Use the properties with a "2" to manipulate the MovieClip in regards to it's "new" registration point that you set with "setRegistration". Note that you must call setRegistration on a MovieClip before trying to manipulate it with the above properties.

Special thanks go out to Robert Penner for originally creating the AS 1 code. I simply ported it over to AS 2 and made the test bed you see below.. credit where credit is due.

17 Comments

  • The AS1 code originally written by Robert Penner is below:


    var MCP = MovieClip.prototype;
    MCP._xreg = MCP._yreg = 0;

    MCP.setPropRel = function (prop, amount) {
    var a = {x:this._xreg, y:this._yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    this[prop] = amount;
    var b = {x:this._xreg, y:this._yreg};
    this.localToGlobal (b);
    this._parent.globalToLocal (b);
    this._x -= b.x - a.x;
    this._y -= b.y - a.y;
    };

    MCP.set_x2 = function (v) {
    var a = {x:this._xreg, y:this._yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    this._x += v - a.x;
    };

    MCP.get_x2 = function () {
    var a = {x:this._xreg, y:this._yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    return a.x;
    };

    MCP.set_y2 = function (v) {
    var a = {x:this._xreg, y:this._yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    this._y += v - a.y;
    };

    MCP.get_y2 = function () {
    var a = {x:this._xreg, y:this._yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    return a.y;
    };

    MCP.set_xscale2 = function (v) {
    this.setPropRel("_xscale", v);
    };

    MCP.get_xscale2 = function () {
    return this._xscale;
    };

    MCP.set_yscale2 = function (v) {
    this.setPropRel("_yscale", v);
    };

    MCP.get_yscale2 = function () {
    return this._yscale;
    };

    MCP.set_rotation2 = function (v) {
    this.setPropRel("_rotation", v);
    };

    MCP.get_rotation2 = function () {
    return this._rotation;
    };

    MCP.get_xmouse2 = function () {
    return this._xmouse - this._xreg;
    };

    MCP.get_ymouse2 = function () {
    return this._ymouse - this._yreg;
    };

    with (MCP) {
    addProperty("_x2", get_x2, set_x2);
    addProperty("_y2", get_y2, set_y2);
    addProperty("_xscale2", get_xscale2, set_xscale2);
    addProperty("_yscale2", get_yscale2, set_yscale2);
    addProperty("_rotation2", get_rotation2, set_rotation2);
    addProperty("_xmouse2", get_xmouse2, null);
    addProperty("_ymouse2", get_ymouse2, null);
    }

    ASSetPropFlags (MCP, null, 1);
    delete MCP;

    // ---

    Don't forget to set mc._xreg, and mc._yreg before using any of the new properties made available (_x2, _rotation2, etc).

     
  • Hi Darren,
    Nice work.
    Just a quickie - you've misspelled DynamicRegistration as DymanicRegistration in the constructor, which is probably why you require users to call setRegistration() first...

    Cheers,
    Ian

     
  • Whoops - that should have been to Darron. :-)

     
  • There's an error in DynamicRegistration.as

    function get _y2 ():Number {
    var a = {x:this.xreg, y:this.yreg};
    this.localToGlobal(a);
    this._parent.globalToLocal(a);
    return a.x;
    }

    this needs to return a.y, not a.x. Just change a.x to a.y and it's all good.

    Great work, by the way. This was just what I needed.

     
  • Nice, I have an alternative version up here: http://jeff.mxdj.com/read/1090157.htm which allows developers to chose on of 9 positions for the registration point, much like the "Create Symbol" dialog box in Flash allows users to choose (Top Left, Top Center, Top Right, etc.)

    The other difference with mine, is that its a static class, meaning it can be applied to any MovieClip (or its subclass), without having to redifine the movie clips class.

     
  • mention abour your Syintax in your site

     
  • how can i get the "classic" properties of my movie clip now?
    var total:DynamicRegistration;

    and after if i do:

    total.nestedMc._x=100;
    >> in the output window :
    there is no property called "nestedMc"

     
  • You can use the ActionScript 1 code that I had pasted.

    I goofed when I made this - The dynamic registration point should've been a mixin class, so you could call:

    com.darronschall.DynamicRegistration.initialize(movieclip);

    I've updated this weblog entry (and the .zip download) to make the class easier to use. Re-download it, and try again. Sorry about that!

     
  • ok thanx darron! it helps a lot

     
  • dude, nice work. i just had to do this all manually last week. this will save me so much time. :)

     
  • Thanks for sharing this solution with us. Would it be easy to modify this script to work with registration coordinates on the root level, or a containing movieclip for that matter? I'm trying to scale two movieclips relative to the same reg point...

     
  • Hello,
    I have tried these code but I found a problem, if I load a movie into the movie clip the new properties are lost


    regards

     
  • hello everyone, i am trying to make a "draggable" object with this example of dynamic registration like in this site http://www.intentionallies.co.jp anyone have any idea or example for this ?? thankz ;)

     
  • It seems I can't do a second setRegistration on a clip after I've applied a _rotation2 tranformation (using the Tween Class).

    The 1st rotation apply correctly; I set another regpoint, do a second rotation but the clip will tween to the regpoint instead of it's intended destination

    Any suggestions anyone?

    Thanks

    multiple.paradox@gmail.com

     
  • (read previous post)
    I've just noticed; it seems the clip uses it's previous position (before the 1st rotate) as it's regpoint, wich is causing my problem...

    I haven't find the reason yet though

     
  • Can I give you a kiss? You made my week! Thanks! Thank you so much for making this!

     
  • Fabulous. Wonderful. So useful!

    Why doesn't Flash have a built in function to change the reg point? Arg!

     

Leave a comment

Flex.org - The Directory for Flex

Archives