Problems in centering image

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

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

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

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.

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
}

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.

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.

No problem, glad to help.