简体   繁体   中英

AS3 Scale parent mc's center to child mc's center

The following code centers the parent clip "mapcontainer.themap" on the state Georgia on load. However, when i try to scale mapcontainer.themap, it doesnt scale around Georgia's center. How do i keep Georgia in the center when scaling?

//This sets the parent clip so it's centered on the child "georgia" clip on load
var yValue:Number = mapcontainer.themap.georgia.y;
var xValue:Number = mapcontainer.themap.georgia.x;
var xDiff:Number = mapcontainer.themap.width/2;
var yDiff:Number = mapcontainer.themap.height/2;
if (xDiff > stage.stageWidth/2 || yDiff > stage.stageHeight/2) {
    xValue = -xValue;
    yValue = -yValue;
}
mapcontainer.themap.x = xValue;
mapcontainer.themap.y = yValue;

//This line?
mapcontainer.themap.scaleX = mapcontainer.themap.scaleY = 2;

Here is a function derived from here

protected function scaleAround(objectToScale:DisplayObject, scalePoint:Point, scaleAmount:Number ):void {
    // scaling will be done relatively
    var relScaleX:Number = scaleAmount / objectToScale.scaleX;
    var relScaleY:Number = scaleAmount / objectToScale.scaleY;
    // map vector to centre point within parent scope

    scalePoint = objectToScale.localToGlobal( scalePoint );
    scalePoint = objectToScale.parent.globalToLocal( scalePoint );
    // current registered postion AB
    var AB:Point = new Point( objectToScale.x, objectToScale.y );
    // CB = AB - scalePoint, objectToScale vector that will scale as it runs from the centre
    var CB:Point = AB.subtract( scalePoint );
    CB.x *= relScaleX;
    CB.y *= relScaleY;
    // recaulate AB, objectToScale will be the adjusted position for the clip
    AB = scalePoint.add( CB );
    // set actual properties
    objectToScale.scaleX *= relScaleX;
    objectToScale.scaleY *= relScaleY;
    objectToScale.x = AB.x;
    objectToScale.y = AB.y;
}

So for your scenario, you could do this:

scaleAround(mapcontainer.themap,new Point(mapcontainer.themap.georgia.x,mapcontainer.themap.georgia.y),2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM