How to change the coordinate origin in Flash's stage with Actionscript?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 20:42:26

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!