Today I’m starting a new kind of post about code snippets to give back to the community some of what I’ve learned.
Sometimes there is the necessity to replace a DisplayObject with another one, preserving the matrix transformation. This is useful when you design some UI elements in Flash authoring as placeholder and want to replace it with another, more complex, object.
The following function simply replace a placeholder DisplayObject with another one, mantaining the same position and, optionally, the same size:
public function switchFromPlaceHolder(_newDO:DisplayObject, _placeHolder:DisplayObject, _sameSize:Boolean=false):void
{
var parentPlaceHolder:DisplayObjectContainer = _placeHolder.parent;
var indexPlaceHolder:int = parentPlaceHolder.getChildIndex(_placeHolder);
parentPlaceHolder.removeChild(_placeHolder);
parentPlaceHolder.addChildAt(_newDO, indexPlaceHolder);
_newDO.x = _placeHolder.x
_newDO.y = _placeHolder.y
if(_sameSize)
{
_newDO.width = _placeHolder.width
_newDO.height = _placeHolder.height
}
}
So, you can design in Flash authoring elements that can be replaced with custom classes, without worrying about the placement.
Hope this will help someone.