# Problems in centering image

**URL:** https://forum.kirupa.com/t/problems-in-centering-image/185072
**Category:** flash
**Created:** [April 10, 2006, 7:20pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072 "2006-04-10T19:20:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pram310](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@pram310](https://forum.kirupa.com/u/pram310)
#### Post date: [April 10, 2006, 7:20pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/1 "2006-04-10T19:20:29Z")

</div>

Hi all

I’ve been working on a dynamic slideshow for some time now. I create an empty Movie Clip imgMC and then load pictures into it. Everything worked fine when the images were all the same size, and the dimensions were hardcoded.

Today I tried the same thing with different size images. I’m calculating the position of my imgMC like so:  
imgMC.\_x = (640 - imgMC.\_width)/2;  
imgMC.\_y = (800 - imgMC.\_height)/2;

Say, picture A is currently loaded in imgMC and is 180 px wide. When I click on link for picture B (160 px wide), and do a trace on imgMC.\_width just after I do a loadMovie, it shows 180 instead of 160.

This basically means that the first time I load an image, there is nothing there, and it always shows 0 on trace. This really throws the X and Y coords way off, and my image isn’t centered on the page. Any pointers anyone?

TIA

---

<div class="post-metadata">

### Author: ![Stratification](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@Stratification](https://forum.kirupa.com/u/Stratification)
#### Post date: [April 10, 2006, 7:26pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/2 "2006-04-10T19:26:37Z")

</div>

You need to do an onLoad event for the clip, otherwise it is executing before anything ever loads as you’ve noticed.

---

<div class="post-metadata">

### Author: ![pram310](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@pram310](https://forum.kirupa.com/u/pram310)
#### Post date: [April 10, 2006, 7:40pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/3 "2006-04-10T19:40:55Z")

</div>

Thanks for your reply Stratification. I’m a little lost, Flash noob you see 🙂

Should I put the code for calculating x and y pos within the onLoad event? I wrote an onLoad function, and traced the \_width, but it looks as if it’s never getting called.

---

<div class="post-metadata">

### Author: ![Stratification](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@Stratification](https://forum.kirupa.com/u/Stratification)
#### Post date: [April 10, 2006, 7:57pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/4 "2006-04-10T19:57:50Z")

</div>

Actually, thinking about it what you may want to do is set up an onEnterFrame function for the loading clip (or a container). Then test the getBytesLoaded against getBytesTotal for the image you’re loading. Be sure to delete the onEnterFrame once you’re finished with it. A rough example (pseudocode, not real code obviously):

if(getBytesLoaded == getBytesTotal){  
calculate the size  
delete the onEnterFrame  
}

---

<div class="post-metadata">

### Author: ![pram310](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@pram310](https://forum.kirupa.com/u/pram310)
#### Post date: [April 10, 2006, 7:59pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/5 "2006-04-10T19:59:07Z")

</div>

Never mind, I took your tip Strat and looked through Flash help. I’m edging closer to centering nirvana I think. Will post l8r if it works out.

---

<div class="post-metadata">

### Author: ![pram310](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@pram310](https://forum.kirupa.com/u/pram310)
#### Post date: [April 10, 2006, 8:20pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/6 "2006-04-10T20:20:36Z")

</div>

Yippee!!! Thanks to Stratification for leading me to the correct solution :thumb:

What I was doing was creating a movie clip, and loading an image into it. Instead, I created a MovieClipLoader object. On the Init function of the loader, I could get the dimensions of the image currently being loaded.

So instead of:

> this.createEmptyMovieClip(“imgMC”, this.getNextHighestDepth());  
> imgMC.loadMovie(sImg);

I put:

> this.createEmptyMovieClip(“imgMC”, this.getNextHighestDepth());  
> var image\_mcl:MovieClipLoader = new MovieClipLoader();  
> var mclListener:Object = new Object();  
> mclListener.onLoadInit = function(target\_mc:MovieClip) {  
> target\_mc.\_x = (640 - target\_mc.\_width)/2;  
> target\_mc.\_y = (800 - target\_mc.\_height)/2;  
> target\_mc.loadMovie(sImg);  
> };  
> image\_mcl.addListener(mclListener);  
> image\_mcl.loadClip(sImg, imgMC);

I also changed every occurrence of the line

> imgMC.loadMovie(sImg);

to

> image\_mcl.loadClip(sImg, imgMC);

That helped me in centering my image without hardcoding. Hope this helps others!

Thanks again Stratification.

---

<div class="post-metadata">

### Author: ![Stratification](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@Stratification](https://forum.kirupa.com/u/Stratification)
#### Post date: [April 10, 2006, 9:00pm UTC](https://forum.kirupa.com/t/problems-in-centering-image/185072/7 "2006-04-10T21:00:23Z")

</div>

No problem, glad to help.
