Skip to main content
Participant
December 12, 2014
Question

How to create two UILoader that can be nested with each other and save the parent UILoader - as3

  • December 12, 2014
  • 1 reply
  • 228 views

Hi all,

I am currently researching on the problem but I can't find any solution in the web.

Here is my problem:

I have two UILoader instance (loader_inner and loader_outer) I need to have loader_inner to be part of the loader_outer. I have also a button that will save all contents on loader_outer. Unluckily, when I trigger the save function, I only got  a white image file. I was expecting that the contents of loader_outer (with loader_inner) to be saved also.

Note: background_1.jpg and rabbit.jpg are present in the directory. loader_outer and loader_inner where manually placed in the scene using UILoader component.



Here is a code snippet:

loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer
.load(new URLRequest("rabbit.jpg"));
loader_outer
.addChild(loader_inner);
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData
.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile
.save(mybyte,"test.jpg");

My expected output should be that background_1.jpg and rabbit.jpg should be saved in an image file and must be positioned like the ones placed in the scene.

Any ideas on this?

Thanks ahead.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 12, 2014

the load method is asynchronous so you don't want to apply that draw method until both loads are complete.  ie, use a complete listener:

loader_inner.addEventListener(Event.COMPLETE.loadcompleteF);

loader_outer.addEventListener(Event.COMPLETE,loadcompleteF);

var loadNum:int=0;

loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer
.load(new URLRequest("rabbit.jpg"));
loader_outer
.addChild(loader_inner);


// you should declare these variables outside this function so there are no gc issues.


function loadcompleteF(e:Event):void{

loadNum++;

if(loadNum==2){
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData
.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile
.save(mybyte,"test.jpg");

}

}