views:

13

answers:

1

Hi,

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.

A: 

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.

PatrickS
The first file is the container.swf (first code above) of the second testfile.swf (second code above) ; it isn't two different example but one! And ADDED_TO_STAGE don't make any problem, it is only a centering problem.
Acti67
More info are: the second code must not be changed, because that could be any animation file... The first code who include in this example the second one must centering the animation included inside.Example : if an animation is 300x300, she must be centered inside the container 800x600, but the animation wouldn't be centered when actionscript define itself the (x, y) values (this works with a movieclip added and placed with Flash directly on the scene).
Acti67
PatrickS