How would you set the registration point on a Sprite or Shape via actionscript (I know this is trivial using Flash, but I need to do it purely in actionscript)
to you mean the index ?
Following up the comments, you can do a quick implementation like below. This is not exactly what you want as you cannot set different alignments for each child. I just didn't want to make it too complicated, it's more like 'working-pseudo-code' to give you an idea ...
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
public class SpriteWithRegistration extends Sprite
{
private var _regV:String = "T";
private var _regH:String = "L";
private var _width:Number = 0;
private var _height:Number = 0;
public function SpriteWithRegistration(width:Number, height:Number, registrationPoint:String = "TL")
{
this.width = height;
this.height = width;
this.registrationPoint = registrationPoint;
}
public function set registrationPoint(p:String):void
{
if(p.length != 2) return;
var regV:String = p.toUpperCase().substr(0, 1);
var regH:String = p.toUpperCase().substr(1, 1);
_regV = (regV == "T" || regV == "C" || regV == "B" ) ? regV : _regV;
_regH = (regH == "L" || regH == "C" || regH == "R" ) ? regH : _regH;
alignChildren();
}
override public function addChild(child:DisplayObject):DisplayObject
{
alignChild(child);
super.addChild(child);
return child;
}
override public function set width(value:Number):void
{
_width = value;
alignChildren();
}
override public function get width():Number
{
return _width;
}
override public function set height(value:Number):void
{
_height = value;
alignChildren();
}
override public function get height():Number
{
return _height;
}
private function alignChildren():void
{
for(var index:int = 0;index < numChildren; index++ )
alignChild(getChildAt(index));
}
private function alignChild(disp:*):void
{
switch(_regH)
{
case "L": disp.x = 0; break;
case "C": disp.x = _width*.5 - disp.width * .5; break;
case "R": disp.x = _width - disp.width; break;
}
switch(_regV)
{
case "T": disp.y = 0; break;
case "C": disp.y = _height*.5 - disp.height * .5; break;
case "B": disp.y = _height - disp.height; break;
}
}
}
}
Depending on why you need it, it might be solved by using DisplayObject.transform.matrix, which returns a Matrix object.
For example, if you wanted to change the point of rotation then you could use the transform's matrix to do that.
The Flash Player API doesn't expose this. I believe this is because Flash actually bakes in the registration point into the shape data when it creates the SWF. Thus, there's no actual registration point to move (instead, you'd move the shape data... if the Flash Player let you edit shape data!).
I always solve this by simply parenting my sprite/shape to another DisplayObject. So, if I have spriteA and want to set its registration point to (15, 35), I'd do this:
var spriteB:Sprite = new Sprite();
spriteB.addChild(spriteA);
spriteA.x = 15;
spriteA.y = 35;
And then from then on refer to spriteB everywhere I was previously referring to spriteA.
Using transform matrix this is possible. Here is a good implementation of this found on this site.
public function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean )
{
//translate movieclip
s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy);
//registration point.
if (showRegistration)
{
var mark:Sprite = new Sprite();
mark.graphics.lineStyle(1, 0x000000);
mark.graphics.moveTo(-5, -5);
mark.graphics.lineTo(5, 5);
mark.graphics.moveTo(-5, 5);
mark.graphics.lineTo(5, -5);
s.parent.addChild(mark);
}
}