Changing MovieClip’s registration point using AS3
January 28, 2010
Leave a comment
Hey all,
here is the simple class that helps you to create a custom MovieClip and you can change its registration point at anytime,
package com.karthik.web.display {
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class KMovieClip extends MovieClip {
protected var _registrationX:Number; // Registration point X
protected var _registrationY:Number; // Registration point Y
protected var _x:Number; // User-defined X
protected var _y:Number; // User-defined Y
protected var setToUpdate:Boolean; // Whether this instance is already set to update on the next Event.RENDER
// CONSTRUCTOR
public function KMovieClip() {
super();
// Reads the current __values to keep them
_x = super.x;
_y = super.y;
_registrationX = 0;
_registrationY = 0;
fixPosition();
}
// INTERNAL functions
protected function fixPosition(): void {
// Using localToGlobal/globalToLocal is less precise than doing it mathematically, but the end result is more accurate inside Flash because it's in sync with Flash's positioning and rotating limitations
var op:Point = new Point(0, 0);
var rp:Point = new Point(_registrationX, _registrationY);
rp = parent.globalToLocal(localToGlobal(rp));
op = parent.globalToLocal(localToGlobal(op));
super.x = _x - (rp.x - op.x);
super.y = _y - (rp.y - op.y);
}
protected function requestPositionFix(): void {
if (Boolean(stage) && !setToUpdate) {
setToUpdate = true;
stage.addEventListener(Event.RENDER, onRender, false, 0, true);
stage.invalidate();
}
}
// EVENT functions ------------------------------------------------------------------------------------------------
protected function onRender(e:Event): void {
stage.removeEventListener(Event.RENDER, onRender);
setToUpdate = false;
fixPosition();
}
// ACCESSOR functions ---------------------------------------------------------------------------------------------
override public function get x(): Number {
return _x;
}
override public function set x(__value:Number): void {
_x = __value;
requestPositionFix();
}
override public function get y(): Number {
return _y;
}
override public function set y(__value:Number): void {
_y = __value;
requestPositionFix();
}
override public function set rotation(__value:Number): void {
super.rotation = __value;
requestPositionFix();
}
override public function set scaleX(__value:Number): void {
super.scaleX = __value;
requestPositionFix();
}
override public function set scaleY(__value:Number): void {
super.scaleY = __value;
requestPositionFix();
}
public function get registrationX(): Number {
return _registrationX;
}
public function set registrationX(__value:Number): void {
_registrationX = __value;
requestPositionFix();
}
public function get registrationY(): Number {
return _registrationY;
}
public function set registrationY(__value:Number): void {
_registrationY = __value;
requestPositionFix();
}
}
}
After creating an instance of this MovieClip class, you can change the registration point as below,
mc.registrationX = 10;
mc.registrationY = 10;