views:

421

answers:

2

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?

+2  A: 

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.

Typeoneerror
Thanks, that's a good one. Although I forgot to mention I wanted to use a conventional 2D reference system, I.E. positive Y's pointing upwards. I guess I'll just have conversion functions.
Petruza
LOL. Rotate the MovieClip 180 degrees? I think you better just get used to Flash being a bit of a weirdo ;)
Typeoneerror
Lol I hope the rotation tip is a joke ;) In fact it's not weirdo at all, most graphic libraries and frameworks use that coordinate system, but it's easier to apply what I (hardly) learned at school with a regular cartesian system.
Petruza
+1  A: 

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.

wmid