PDA

View Full Version : load xml images-->Array-->Duplicate Array: AHAA!



numediaweb
January 9th, 2009, 05:10 AM
Hello Kirupians!
[Summary]
I want to load images from an XML, then store the images inside an array, then duplicate that array into other clone arrays so that I can addChild() copies of the images into stage whenever i want (and be able to change their properties).
Basicly i have to create two duplicates; big_img and icon_image

[SYMPTOMS]
Everything is loaded fine;the big_imgloads and positions correctly, but, when i try to load icon_image the first big_img disappears! as if the new array has hasn't duplicated the original array (as if it's only a shortcut, not a real copy).

The cody thing:

// total images read from XML
var axiLength=Accessoir.length();
// main image container array
var axxARR:Array = new Array();
// add images to array
for (var k=0; k<axiLength; k++) {
var axxLoader:Loader = new Loader();
axxLoader.load(new URLRequest(Accessoir.PHOTO.text()[k]));
axxARR.push(axxLoader);
axxLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, axxLoaded);
}
function axxLoaded(e:Event):void {
// show big image
if (axiLength==axxARR.length) {
axxShow(0);
}
}
function axxShow(ID_AXX:Number) {
// duplicate images array
var axxImage:Array=axxARR.concat(axxARR);
// reposition it
axxImage[ID_AXX].x = product_details_swf.axx.x+(axxImage[ID_AXX].width/2)-5.5;
// Add init image to accessoires
axxImage[ID_AXX].name="axxImage";
product_details_swf.axx.addChild(axxImage[ID_AXX]);
}
// here i got an event listner to a button, once rolle over is triged it
// should make small copies of the whole array images and put them on stage
//....
function rollover_button(ID_AXX:Number) {
for (var i=0; i<axiLength; i++) {
axxCreate(i);
}
function axxCreate(ID_AXX:Number) {
var axxPic:Array=axxARR.concat();
imageResizer(axxPic[ID_AXX], 50, 50);
axxPic[ID_AXX].name="axxPic_"+ID_AXX;
axxPic[ID_AXX].x=product_details_swf.axx.getChildByName("axxBG_"+ID_AXX).x+axxPicSpacingX;
axxPic[ID_AXX].y=product_details_swf.axx.getChildByName("axxBG_"+ID_AXX).y+axxPicSpacingY;
product_details_swf.axx.addChild(axxPic[ID_AXX]);
}
}


[MORE INFORMATION]
I tried array duplication using; concat and slice but not work, it doesn't creat copies of the orriginal array, but just a shortcut to it!

thanx for any tips!

Krilnon
January 9th, 2009, 05:26 AM
You're pretty much right about the array duplication not really copying your array elements. You're just making another array with references to the same objects.

I can think of a few of ways to get what you want. First, you could use a URLLoader to load the images as raw data, and the use the Loader class instance method loadBytes to load the images from the ByteArray that you get from using the URLLoader. A disadvantage of that is that loadBytes is still asynchronous, so you'd have to set up listeners to determine when the image copy was finished.

A second method, since you're just dealing with non-moving images, would be to use the Bitmap class field bitmapData to access the BitmapData that is the source of your image. You could then use that instance's clone method to make a copy of the BitmapData so that you could change it without changing the other instances.

The last way that you could try would be to use something like senocular's duplicateMovieClip replacement. It's kind of overkill for what you're doing, but it would probably work too. See: http://www.kirupa.com/forum/showthread.php?p=1939827#post1939827

numediaweb
January 9th, 2009, 06:10 AM
Thank you Krilnon for puting some light on my road:
I think Adobe is missing something here about object cloning: a lot of people are upset about this issue..

please could you explain the first solution a litel bit more..

numediaweb
January 9th, 2009, 12:16 PM
[Resolved]
I did it the dirty way ;-)

for (var k=0; k<axiLength; k++) {
//load big image
var axxBIGloader:Loader = new Loader();
axxBIGloader.load(new URLRequest(G_ProductPicture+productData.Accessoire s.Accessoir.PHOTO.text()[k]));
axxBIG.push(axxBIGloader);
axxBIGloader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, axxBIGloaded);
// load icon images
var axxICOloader:Loader = new Loader();
axxICOloader.load(new URLRequest(G_ProductPicture+productData.Accessoire s.Accessoir.PHOTO.text()[k]));
axxICO.push(axxICOloader);
axxBIGloader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, axxICOloaded);
}
function axxBIGloaded(e:Event):void {
if (axiLength==axxBIG.length) {
if (axxFirst==false) {
axxShow(0);
axxFirst=true;
}
}
}
function axxICOloaded(e:Event):void {
// do nothing !
}

gonna come back to clean it when i got more time, but, it's working now.. even it's dirty coded!