Question

I think I did this before but can't find the code.

Flash as many other graphical frameworks use the top-left corner as the coordinate origin (0,0) because it's how the underlying memory model is by convention.

But it would be really simpler for my calculations if the origin was in the center of the stage, because all the game revolves around the center and uses a lot of trigonometry, angles, etc.

Is there some built-in method like Stage::setOrigin( uint, uint ); or something like that?

Was it helpful?

Solution

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. Then when you add your game objects to that instead. Add your game objects at 0,0 inside of that clip and they will be centered on the stage.

OTHER TIPS

Create a class that overrides the x and y setters and getters to handle the calculations. Any MovieClips on stage should extends this new class.

package {
    // imports

    public class MyDisplayObject extends DisplayObject
    {

        private var originX:Number = 0;
        private var originY:Number = 0;

        public function MyDisplayObject() {
            // constructor stuff
            originX = stage.stageWidth / 2;
            originY = stage.stageHeight / 2;
        }

        override public function set x($x:Number):Void {
            super.x = originX + $x; // use super to avoid these setters and getters
        }

        override public function set y($y:Number):Void {
            super.y = originY + $y;
        }

        override public function get x():Number {
            return super.x - originX;
        }

        override public function get y():Number {
            return super.y - originY;
        }
    }
}

Bonus: you can change the origin values whenever you want, so it doesn't have to be at the center of the stage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top