开发者

AS3 : Center an animation loaded in one other

I have a question to center an animation within its container.

This is the container or loader:

    public function Main():void
    {
        trace("Constructor...");

        this.addEventListener(Event.ADDED_TO_STAGE, this.addEvent);
    }

    public function addEvent(e:Event):void
    {
        trace("AddedToStage");

        this.stage.scaleMode = StageScaleMode.NO_SCALE;
        this.stage.align = StageAlign.TOP_LEFT;

        var url = new URLRequest("testfile.swf");
        movie = new Loader();
        movie.contentLoaderInfo.addEventListener(Event.COMPLETE, this.eventComplete);
        this.stage.addChild(movie);
        movie.load(url);
    }

    public function eventComplete(e:Event):void
    {
        trace("Complete...");

        movie.x 开发者_高级运维= (this.stage.stageWidth - movie.width) * 0.5;
        movie.y = (this.stage.stageHeight - movie.height) * 0.5;
    }

This work perfectly with the objects that are on the scene, but not with objects that are added with actionscript... like this:

    public function Main():void
    {
        trace("Constructor of included file!");

        stage.scaleMode = StageScaleMode.NO_SCALE;

        var movieclip = new symbol_an();
        stage.addChild(movieclip);
    }

Do you have a solution?

Thank you, regards.


What do you mean by it's not working , your second function doesn't show much... In the second part of your code you can do the same as in the first one, basically get the width of the container and its child , substract then divide the result by two. For it to work, you have to make sure that the width or height properties actually have a value, this depends on your particular application so it's difficult to help you without having more info.

In the second part , you don't have an ADDED_TO_STAGE event listener , this could possibly throw an error because the stage value could be null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜