Hey there!
I am currently programming a image gallery for a webpage of mine. And I feel like my AS3 code is written very clumsy or something, cause it’s not working as it should do.
My problem with this code is that in my XML file I’ve got a tag named osrc and one called src. The src tag contains the URL for the thumbnail wich is shown at load, and the osrc is the URL for the full-size image.
What my mind tells me is the logical thing happening in this code is that it loads an image from src and adds it to the stage (twelve at the time). And then it should add osrc in the EventListener…
BUT!! What happens?? I’ll tell you… everytime i reload the page, the images will change places randomly! :S but the osrc will be in the same place every time, wich means that when I click one of the images it’ll trace the osrc… but the wrong one… If you know what I mean… :S soo… hum… help?
The code is not finished as you might see.
Here’s the code for the loading-part:
(translation of certain parts in the code. Bilder=Images, Bild=Image)
loadBilder(1);
var bild_index:Number = 0;
var oSRC:Array = new Array();
function loadBilderCompleted(e:Event):void {
try {
var bilderXML:XML = new XML(e.target.data);
var xmlList:XMLList = bilderXML.bild;
//trace(xmlList.length());
while(numChildren > 0) {
removeChildAt(numChildren-1);
}
bild_index = 0;
var xmlCount:Number = Number(xmlList.length());
var ii:Number = 0;
var Loaders:Array = new Array;
while(ii < xmlList.length()) {
Loaders[ii] = new imageLoader();
addChild(Loaders[ii]);
Loaders[ii].alpha = 75;
if(ii < 4) {
Loaders[ii].x = ii * 210;
Loaders[ii].y = 0;
} else if(ii < 8) {
Loaders[ii].x = (ii - 4) * 210;
Loaders[ii].y = 150;
} else if(ii < 12) {
Loaders[ii].x = (ii - 8) * 210;
Loaders[ii].y = 300;
};
ii += 1;
}
for each (var srcElement:XML in xmlList) {
/*this.text_antal.appendText(srcElement.description);
this.text_antal.appendText(" - ");
this.text_antal.appendText(srcElement.src);
this.text_antal.appendText(String(index));
this.text_antal.appendText("
");*/
oSRC.push(srcElement.osrc);
try {
var imageURLRequest:Array = new Array();
imageURLRequest[bild_index]= new URLRequest(srcElement.src);
var myImageLoader:Array = new Array();
myImageLoader[bild_index] = new Loader();
myImageLoader[bild_index].load(imageURLRequest[bild_index]);
} catch(e:Error) {
trace("URLRequest and Loader failure!");
}
try {
myImageLoader[bild_index].contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
} catch(e:Error) {
trace("Could not create Event Listener for myImageLoader");
}
function imageLoaded(e:Event):void {
try {
var myBitmapData:Array = new Array();
try{
myBitmapData[bild_index] = new BitmapData(e.target.content.width, e.target.content.height);
var imgLoaded:Array = new Array();
imgLoaded[bild_index] = e.target.content;
myBitmapData[bild_index].draw(imgLoaded[bild_index]); } catch(e:Error) { trace(e);}
} catch(e:Error) {
trace("Couldn't create myBitmapData properly")
}
try {
var myBitmap:Array = new Array();
myBitmap[bild_index] = new Bitmap;
myBitmap[bild_index].bitmapData = myBitmapData[bild_index];
} catch(e:Error) {
trace("Couldn't create myBitmap properly")
}
try {
var mySprite:Array = new Array();
mySprite[bild_index] = new MovieClip;
addChild(mySprite[bild_index]);
removeChild(Loaders[bild_index]);
mySprite[bild_index].addChild(myBitmap[bild_index]);
var i:Number = mySprite[bild_index].height / 127.8;
mySprite[bild_index].osrc = oSRC[bild_index];
mySprite[bild_index].name = "mySprite" + bild_index;
mySprite[bild_index].width = mySprite[bild_index].width / i;
mySprite[bild_index].height = mySprite[bild_index].height / i;
if(bild_index < 4) {
mySprite[bild_index].x = bild_index * 210;
mySprite[bild_index].y = 0;
} else if(bild_index < 8) {
mySprite[bild_index].x = (bild_index - 4) * 210;
mySprite[bild_index].y = 150;
} else if(bild_index < 12) {
mySprite[bild_index].x = (bild_index - 8) * 210;
mySprite[bild_index].y = 300;
};
mySprite[bild_index].buttonMode = true;
mySprite[bild_index].useHandCursor = true;
mySprite[bild_index].addEventListener(MouseEvent.CLICK, imgClick);
} catch(e:Error) {
trace("Couldn't create mySprite properly")
}
bild_index += 1;
}
};
} catch(e:Error) {
trace("xml failure!!");
}
}
function imgClick(e:Event) {
trace(e.target.name);
trace(e.target.osrc);
}
//trace(String(bild_index) + "mamma")
function loadBilder(page:Number):void {
var bilderURLRequest:URLRequest=new URLRequest();
// bilderURLRequest.url="bilder.php?page=" + page;
bilderURLRequest.url="bilder.xml";
var bilderLoader:URLLoader=new URLLoader();
bilderLoader.addEventListener(Event.COMPLETE, loadBilderCompleted);
try
{
bilderLoader.load(bilderURLRequest);
}
catch (error:Error)
{
trace('Error while loading xml-file');
}
}
I know that my code might be vary confusing, or very clumsy… I’m pretty new at AS3… and this is the only way I’ve gotten it to… hum… nearly work :trout: