PDA

View Full Version : How to centre align loaded images


maninderv
02-26-2007, 07:19 PM
Hi Everyone,

I am very very new to flash and as my first project, I am building a swf that is supposed to load external images through the xml file into my own swf.

For simplicity, lets assume that I wanted to load an image into my swf file.

I create an empty movie clip (ctrl+f8) then drag it onto the stage and give it an instance name of parent_mc. Then press f9 to go to the actions panel and type the following -

_root.parent_mc.createEmptyMovieClip("child_mc", 99);
_root.parent_mc.child_mc._lockroot = true;
_root.parent_mc.loadMovie("http://localhost/img/1_medium.jpg", _root.parent_mc.child_mc);

_root.parent_mc._xscale = 50;
_root.parent_mc._yscale = 50;

The image shows up fine.

Now my issues -

1) I want parent_mc to be 300 x 300. I couldnt get it to work by giving it _width & _height. So, I did a hit & trial with _xscale and _yscale that I still dont understand. Why can't I give parent_mc a height and width?

2) Now, all images that are big load fine, but if there is some image that is 200 x 200, I want it to be centred. How do I handle that?

Any help would be appreciated.

Thanks,
Mandy

wvxvw
02-27-2007, 01:46 PM
Firstly, check out the MovieClipLoader class. It adds more options to trace and handle downloads.
Secondly, here's how you may center one clip relative to another one:
var one_mc:MovieClip = _root.createEmptyMovieClip("one_mc", _root.getNextHighestDepth());
drawRect(one_mc, 400, 500);
var two_mc:MovieClip = _root.createEmptyMovieClip("two_mc", _root.getNextHighestDepth());
drawRect(two_mc, 200, 300);
center(one_mc, two_mc);
function drawRect(_mc:MovieClip, xx:Number, yy:Number) {
_mc.beginFill(Math.random()*0xffffff, 100);
_mc.moveTo(0, 0);
_mc.lineTo(xx, 0);
_mc.lineTo(xx, yy);
_mc.lineTo(0, yy);
_mc.lineTo(0, 0);
_mc.endFill();
}
function center(_mc1:MovieClip, _mc2:MovieClip) {
_mc2._x = Stage.width/2-_mc2._width/2;
_mc2._y = Stage.height/2-_mc2._height/2;
_mc1._x = _mc2._x+_mc2._width/2-_mc1._width/2;
_mc1._y = _mc2._y+_mc2._height/2-_mc1._height/2;
}