1
votes

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?

1
Could you say WHY you believe that you need to shift your origin? Actually, you almost certainly DON'T have to -- and there are probably simpler solutions to your problem than creating a 'pseudo-stage' MovieClip. But WHAT exactly are you trying to accomplish?Craig
Well I'm just more comfortable that way. I'm making a physics simulation which requires constant outputting of the coordinates of a moving body, and it's just much easier to visualize when the origin is at the center of the stage.user3073511

1 Answers

0
votes

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.