# Center a dynamic created movie clip with action script

**URL:** https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760
**Category:** flash
**Created:** [April 7, 2006, 2:45pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760 "2006-04-07T14:45:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kungFuChris](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@kungFuChris](https://forum.kirupa.com/u/kungFuChris)
#### Post date: [April 7, 2006, 2:45pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/1 "2006-04-07T14:45:43Z")

</div>

Hi all,

I have got a movie clip that is dynamically created with actionscript  
I then load an external swf into that movieclip. My problem is having it centered when I view it in my browser…

How do place I center that dynamic created movie clip?  
I was trying this but it’s not seeming to work.

```auto
var container:MovieClip = this.createEmptyMovieClip("container_mc", 0);
container._x =Stage.width/2;
container._y =Stage.height/2;

```

Any help appreciated. Thanks

---

<div class="post-metadata">

### Author: ![Juduth](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/juduth/32/2335_2.png) [@Juduth](https://forum.kirupa.com/u/Juduth)
#### Post date: [April 7, 2006, 2:50pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/2 "2006-04-07T14:50:49Z")

</div>

The loader fits in the middle of the Stage but it loads from the relative x=0 y=0 from the loader (the upper-leftcorner of it :D). I guess you will have to adjust the loader component manually depending on the swf height and width.

---

<div class="post-metadata">

### Author: ![kungFuChris](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@kungFuChris](https://forum.kirupa.com/u/kungFuChris)
#### Post date: [April 7, 2006, 2:56pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/3 "2006-04-07T14:56:43Z")

</div>

got it… thanks…

---

<div class="post-metadata">

### Author: ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)
#### Post date: [April 7, 2006, 3:06pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/4 "2006-04-07T15:06:17Z")

</div>

You could also align the container with AS, but the way you had it, it would move the top left corner to the center, not center the clip. For that you’d want something more like this:

```auto
var container:MovieClip = this.createEmptyMovieClip("container_mc", 0);
container._x =(Stage.width/2)-(this._width/2);
container._y =(Stage.height/2)-(this._height/2);

```

---

<div class="post-metadata">

### Author: ![Juduth](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/juduth/32/2335_2.png) [@Juduth](https://forum.kirupa.com/u/Juduth)
#### Post date: [April 7, 2006, 3:08pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/5 "2006-04-07T15:08:42Z")

</div>

That’s the correct way, I used to do it manually. But this code is the cool way 😃

---

<div class="post-metadata">

### Author: ![kungFuChris](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@kungFuChris](https://forum.kirupa.com/u/kungFuChris)
#### Post date: [April 7, 2006, 3:28pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/6 "2006-04-07T15:28:09Z")

</div>

Hi Defective,

I noticed the error I was making in my code when i viewed yours… the difference was in the width vs the \_width property.

I am still having problems with regards to centering the “container” after I load an external swf into it. It just does not center.

when I do a trace of this.\_width it returns undefined. Is this.\_width suppose to reference the loaded swf?  
when I trace container.\_width after the enternal swf has been loaded into the container it returns 0

One thing to keep in mind is that the width of the loaded external swf vary for each one…

How can I work around all of this.

> [@Defective](#):
>
> You could also align the container with AS, but the way you had it, it would move the top left corner to the center, not center the clip. For that you’d want something more like this:  
> ActionScript Code:  
> [FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] container:[COLOR=#0000ff]MovieClip[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]createEmptyMovieClip[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]“container\_mc”[/COLOR], [COLOR=#000080]0[/COLOR][COLOR=#000000])[/COLOR];  
> container.[COLOR=#0000ff]\_x[/COLOR] =COLOR=#000000[/COLOR]-COLOR=#000000[/COLOR];  
> container.[COLOR=#0000ff]\_y[/COLOR] =COLOR=#000000[/COLOR]-COLOR=#000000[/COLOR];  
> [/LEFT]  
> [/FONT]

---

<div class="post-metadata">

### Author: ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)
#### Post date: [April 7, 2006, 3:42pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/7 "2006-04-07T15:42:06Z")

</div>

use container.\_width, but it will not be the correct value until the external clip has loaded fully into the container. If your using loadMovie, try changing your code to use movieClipLoader instead, it has more control of the loaded clips and the ability to preload as well.

---

<div class="post-metadata">

### Author: ![kungFuChris](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@kungFuChris](https://forum.kirupa.com/u/kungFuChris)
#### Post date: [April 7, 2006, 10:39pm UTC](https://forum.kirupa.com/t/center-a-dynamic-created-movie-clip-with-action-script/184760/8 "2006-04-07T22:39:53Z")

</div>

Hey defective, i am using the movie clip loader class… but was getting the same result as before until I placed the code in onLoadInit = function(){}; seem like I needed to make sure the first frame in the loaded swf was read…

Thanks… you explanation lead me to this…

> [@Defective](#):
>
> use container.\_width, but it will not be the correct value until the external clip has loaded fully into the container. If your using loadMovie, try changing your code to use movieClipLoader instead, it has more control of the loaded clips and the ability to preload as well.
