Надеюсь, это кому-нибудь поможет. У инфраструктуры Starling есть фантастический метод, называемый «alignPivot ()». Я взял их концепцию и адаптировал ее к Flash DisplayList. Вы по сути говорите спрайту изменить его регистрацию на левую, правую, центральную, верхнюю и нижнюю. Этот код помещает ваш спрайт в контейнерный спрайт и позиционирует себя соответствующим образом. Возвращает спрайт контейнера с оригинальным спрайтом, содержащимся внутри.
Код хранится в отдельном названном, называемом «Позиция».
public static function alignPivot(s:DisplayObject, horizontalAlign: String = "center", verticalAlign: String = "center", showRegistration: Boolean = false, _color: uint = 0x000000): Sprite {
//create a container sprite to house the sprite you'd like to move
var _container: Sprite = new Sprite();
//add your sprite to the continer sprite (the container sprite is what will be returned)
_container.addChild(s);
//using getBounds(), find the x,y,width,and height of your sprite within the continer.
var bounds:Rectangle = _container.getBounds(s);
//create variables for x and y cooridnates
var xVal: Number;
var yVal: Number;
//I have a separate class called Align which contains public static constants for positiong.
//Check the values passed above and get the correct x value;
if (horizontalAlign == Align.LEFT) xVal = -bounds.x;
else if (horizontalAlign == Align.CENTER) xVal = -bounds.x - bounds.width * .5;
else if (horizontalAlign == Align.RIGHT) xVal = -bounds.x - bounds.width;
else throw new ArgumentError("Invalid horizontal alignment: " + horizontalAlign);
//Check the values passed above and get the correct y value;
if (verticalAlign == Align.TOP) yVal = -bounds.y;
else if (verticalAlign == Align.CENTER) yVal = -bounds.y - bounds.height * .5;
else if (verticalAlign == Align.BOTTOM) yVal = -bounds.y - bounds.height;
else throw new ArgumentError("Invalid vertical alignment: " + verticalAlign);
//apply the new x and y cooridnates to your sprite (the one moving within the container we created above)
s.x = xVal;
s.y = yVal;
//optional - this will create a small X at the 0,0 position of the container.
//This is helpful if you want to see where your registration points are
if (showRegistration) {
var mark: Sprite = new Sprite();
mark.graphics.lineStyle(1, _color);
mark.graphics.moveTo(-5, -5);
mark.graphics.lineTo(5, 5);
mark.graphics.moveTo(-5, 5);
mark.graphics.lineTo(5, -5);
_container.addChild(mark);
}
//return your contianer sprite
//This will replace the sprite that you passed in the first parameter.
//That sprite is inside the container, so you won't notice
return _container;
}
Реализация:
//Create your sprite
var _holder: Sprite = new Sprite();
//Create a shape to put in your sprite
var my_shape: Shape = new Shape();
my_shape.graphics.beginFill(0x000000);
my_shape.graphics.drawRect(0, 0, 200, 100);
my_shape.graphics.endFill();
//Add the shape to your sprite
_holder.addChild(my_shape);
//Align the holder (must be done AFTER all children have been added)
//This will put the registration point at the top-center of the sprite.
//_holder is replaced by a sprite (container) that contains _holder.
//The _holder inside the container is positioned appropriately.
_holder = Position.alignPivot(_holder,Align.CENTER, Align.TOP);
//Add the new sprite to your stage.
this.addChild(_holder);
Список констант, поэтому вам не нужно писать их самостоятельно:
public static const CENTER:String = "center";
public static const LEFT:String = "left";
public static const RIGHT:String = "right";
public static const TOP:String = "top";
public static const BOTTOM:String = "bottom";