简体   繁体   中英

Actionscript 3 - how to add MovieClip to the stage as root object?

I'm trying to learn Actionscript 3. I need to shift my origin from the top left corner to the center of the stage. I found this question answered on stackoverflow : How to change the coordinate origin in Flash's stage with Actionscript?

The suggestion is, "Create a MovieClip or Sprite and add that to the stage as your root object (instead of adding to the Stage) at stage.width/2, stage.height/2."

What does this mean? What is a root object? How do I add a MC as a root object to the stage?

When you launch a swf you are basically launching a MovieClip. (I'm probably going to get ripped a new one for this analogy). So when you write this

var myMC:MovieClip = new MovieClip();
addChild(myMC);

You are adding a MovieClip to your movies root/stage. Since it is not possible to truly alter the origin of the root/stage, that post is suggesting is that you do the next best thing. By creating another MovieClip and adding to your root/stage like this

var fauxRoot:MovieClip = new MovieClip();
fauxRoot.y = stage.stageHeight/2;
fauxRoot.x = stage.stageWidth/2;
addChild(fauxRoot);

Now that you have centered the fauxRoot MovieClip in your root/stage, you can add all your elements to the fauxRoot instead of your root/stage. Since the fauxRoot is centered in the main root/stage it's 0,0 will be in the center. An Example of adding a button might be

var uiButton:Button = new Button();
uiButton.x = uiButton.width/2;
uiButton.y = uiButton.height/2;
fauxRoot.addChild(uiButton);

The button should now be centered in the middle of your screen. Hope this helps a little.

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