user979331
user979331

Reputation: 11851

AS3 Center a MovieClip

I have a stage that is 1920 in width, the space I am trying to center my MovieClip on is 1520. My MovieClip changes based of user selections, so it could be 1023.9 or 396.8 I am trying to center this movie clip in my 1520 place. How would I do this?

I have tried the following:

floorplanMCAG.x = 1520 - floorplanMCAG.width;

But my 1023.9 MovieClip is too much to the left and my 396.8 MovieClip is a little to the right

Here is how my MovieClip is created:

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);
}

Here is modelInformationAG

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

Upvotes: 0

Views: 506

Answers (1)

Patang
Patang

Reputation: 314

the formula for centering your movieclip depends on where the moviclip has its registration point set. theoratically the registration point could be anywhere, but here for the 2 most common situations:

if it's set in top left corner of the mc:

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

if the registration point is in the center:

floorplanMCAG.x = stage.stageWidth / 2;

EDIT:

ok, I guess this should center your movieClip:

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

Upvotes: 2

Related Questions