ActionScript to change Registration Point of Movie Clip for Dynamically loaded Movie


Below is the code I found on some forum that help in changing the registration point of dynamically loaded Images, that help in rotation of movieclip. Basically it only create new _xreg and _yreg points that need to be adjust to change coordinate system before you rotate image/movieclip using _rotation2 variable.

It works for Action Script 2.0. But should work in AS 3.0 as well.

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;