I’m using Kirupa’s XML Photo Gallery. Anyway, I’ve added a resize function to it (to resize the images so they stay inside the stage). I’ve put it inside the nextImage() function. If I put it in firstImage(), nothing shows up when I publish. I don’t get it. But if I remove the function from firstImage(), and leave it in nextImage(), then the first image is always too large to fit in the stage (it won’t resize!) and when I press the next button, the next image is always properly resized, as well as all the ones after it. The code I use is below:
stop();
var halfDisplayW:Number = Stage.width/2;
var halfDisplayH:Number = Stage.height/2;
var halfImageH:Number;
var halfImageW:Number;
var thisWidth:Number;
var thisHeight:Number;
var maximumHeight:Number;//height to which movieclip to be resized
var maximumWidth:Number;//width to which movieclip to be resized
var oldx:Number;
var oldy:Number;
var ratio:Number;
var myClip:MovieClip = picture.createEmptyMovieClip("holder", 1);
loadXML = function (loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
description = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
}
firstImage();
} else {
content = "file not loaded!";
}
};
var xmlData:XML = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("images.xml");
var listen:Object = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}
};
Key.addListener(listen);
previous_btn.onRelease = function() {
prevImage();
};
next_btn.onRelease = function() {
nextImage();
};
/////////////////////////////////////
var p:Number = 0;
this.onEnterFrame = function() {
filesize = picture.holder.getBytesTotal();
loaded = picture.holder.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
}
}
halfImageW = myClip._width/2;
halfImageH = myClip._height/2;
myClip._x = (halfDisplayW-halfImageW);
myClip._y = (halfDisplayH-halfImageH);
};
nextImage = function () {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
picture._alpha = 0;
picture.holder.loadMovie(image[p],1);
desc_txt.text = "Image Description: "+description[p];
picture_num();
}
}
};
prevImage = function () {
if (p>0) {
p--;
picture._alpha = 0;
picture.holder.loadMovie(image[p],1);
desc_txt.text = "Image Description: "+description[p];
picture_num();
}
};
firstImage = function () {
if (loaded == filesize) {
picture._alpha = 0;
picture.holder.loadMovie(image[0],1);
desc_txt.text = "Image Description: "+description[0];
picture_num();
_root.resizeImage(_root.picture.holder);
}
};
picture_num = function () {
current_pos = p+1;
pos_txt.text = current_pos+" of "+total;
};
resizeImage = function (target_mc:MovieClip) {
_root.thisHeight = target_mc._height;
_root.thisWidth = target_mc._width;
_root.maximumHeight = Stage.height-40;
_root.maximumWidth = Stage.width-40;
_root.ratio = thisHeight/thisWidth;
//calculation ratio to which resize takes place
if (thisWidth>maximumWidth) {
thisWidth = maximumWidth;
thisHeight = Math.round(thisWidth*ratio);
}
if (thisHeight>maximumHeight) {
thisHeight = maximumHeight;
thisWidth = Math.round(thisHeight/ratio);
}
target_mc._width = thisWidth;
target_mc._height = thisHeight;
};