AS3中心是一个MovieClip

时间:2018-01-18 15:19:59

标签: actionscript-3 flash

我有一个宽度为1920的舞台,我试图将我的MovieClip放在中间的空间是1520.我的MovieClip根据用户选择而改变,所以它可能是1023.9或396.8我试图将这个影片剪辑放在中心我的1520个地方。我该怎么做?

我尝试了以下内容:

floorplanMCAG.x = 1520 - floorplanMCAG.width;

但是我的1023.9 MovieClip太多了,我的396.8 MovieClip有点偏右

以下是我的MovieClip的创建方式:

loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFloorplanAG);
loader.load(new URLRequest("images/" + modelAG.floorplan));

var floorplanMCAG:MovieClip = new MovieClip();

function loadFloorplanAG(e:Event):void
{
    floorplanMCAG.addChild(loader);

    floorplanMCAG.height = 468;

    floorplanMCAG.y = 570;

    floorplanMCAG.scaleX = floorplanMCAG.scaleY;

    floorplanMCAG.x = stage.stageWidth / 2;

    trace(floorplanMCAG.width);

    map_boundsFLRPLSAG = new Rectangle(520,570,floorplanMCAG.width,floorplanMCAG.height) // this defines the area in which the map should fit

    min_zoomFLRPLSAG = floorplanMCAG.scaleX;    

    //Zoom and Drag Event Listeners

    stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginFLRPLSAG)
    stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMovedFLRPLSAG)
    stage.addEventListener(TouchEvent.TOUCH_END, touchEndFLRPLSAG)

    modelInformationAG.addChild(floorplanMCAG);

    modelInformationAG.setChildIndex(floorplanMCAG, 0);
}

这是modelInformationAG

var modelInformationAG:MovieClip = new MovieClip();
modelInformationAG.graphics.drawRect(0, 0, 1920, 1080);

1 个答案:

答案 0 :(得分:2)

使影片剪辑居中的公式取决于moviclip设置注册点的位置。理论上,注册点可以在任何地方,但这里有两种最常见的情况:

如果它设置在mc的左上角:

floorplanMCAG.x = (stage.stageWidth - floorplanMCAG.width) / 2;

如果注册点位于中心:

floorplanMCAG.x = stage.stageWidth / 2;

编辑:

好吧,我想这应该以你的movieClip为中心:

floorplanMCAG.x = (modelInformationAG.width - floorplanMCAG.width) / 2;