PDA

View Full Version : Best practice for preloaders



costa
May 30th, 2007, 04:04 AM
Dear all,

I want to start dealing with preloaders for my project. I do not have enough knowledge to do it without asking. So I hope you can advice me.

This is the situation:

1. I have to load a XML file which is around 190K. I am using URLRequest, URLLoader and Event.COMPLETE
2. Now I have around 50 small movie clips in the library which I load dynamically using addChild. According to some articles is better to load the resources from external folders, this is for security reasons. So I am considering to load them in that way but this means I have to load 50 movie clips. I do not know if it is possible load a folder at once.

I will appreciate the help you can give me. Advices and recommendations will be welcome.

Regards.

mathew.er
May 30th, 2007, 05:56 AM
If those MCs are in the library, then they are embed in the one .swf file and loaded at start.

Also putting some files in a folder won't do anything with the security at all. Every part of the content has to be in a public web space for the client to load it and it's urls are no secret.

costa
May 30th, 2007, 04:23 PM
Mathew I got your idea to put all my MCs in a single swf, thanks. About security I will open a new post to talk about it.
Could you recommend a good tutorial related to preloaders in AS3, specifically for XML and swf?

Thanks in advance

costa
May 30th, 2007, 05:17 PM
I found in the tip section a senocular's article "Loading External SWFs with Loader" explaining the loading process for swf. Could be used the same technique to preload XML files?


Edited: Ok, I tested it and I got this error

Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type.

So, I think the answer is NO

Thanks

costa
May 30th, 2007, 06:00 PM
After playing with senocular example and information from Adobe I did this fist attempt to preload a XML file. Please tell me if is ok


var myXML:XML = new XML();
var xml_URL:String = "myFile.xml";
var xmlRequest:URLRequest = new URLRequest(xml_URL);
var xmlLoader:URLLoader = new URLLoader(xmlRequest);

xmlLoader.addEventListener( Event.COMPLETE, xmlHandleComplete );
xmlLoader.addEventListener( ProgressEvent.PROGRESS, xmlHandleProgress );

function xmlHandleComplete( event:Event ):void {
try {
myXML = new XML( event.target.data );
trace("Ready")
//gotoAndStop("Start");
} catch (eType:TypeError) {
trace( "Error" );
trace( eType.message );
}
}

function xmlHandleProgress( event:ProgressEvent ):void {
var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
percentLoaded = Math.round(percentLoaded * 100);
trace("Loading: "+percentLoaded+"%");

}
stop();

costa
May 30th, 2007, 07:56 PM
It seems the XML preloader is working well but I have some doubts about the loaded swf file containing my graphic resources.

1. Once the swf file is loaded using senocular example, it will remain on stage or only in memory?
2. When I have my resources in the Library I used this code to add them on stage:


var classRef:Class = getDefinitionByName(nameMC) as Class;
var instance:Object = new classRef();
container.addChild(DisplayObject(instance));

where nameMc holds the name of the movie clip I want.

Now, after loading the swf containing all my movie clips, how do I add them to stage? I have tried all combinations using the var holding the swf but I was not lucky


Thanks in advance for your help.

mathew.er
May 30th, 2007, 08:51 PM
You didn't understand me... you don't need to load anything, if the MC is already in the library. Its embed... no preloading is necessary or even possible.

costa
May 30th, 2007, 09:06 PM
You didn't understand me... you don't need to load anything, if the MC is already in the library. Its embed... no preloading is necessary or even possible.

Mathew, I understood but according to everybody it is not possible to preload dynamic content, I mean content in the library with linkable option checked, only if you put all the content in a frame before to use it (as you said) or load it from an external file, also in a different frame.

So, I think is more appropriate to load the content from an external final. What do you think?

Thanks for taking time to answer.

costa
May 31st, 2007, 03:03 AM
Ok, finally I was able to preload a swf file with all my assets in put them in a container to be render (thanks to senocular for the preloading code). This is the code:


var request:URLRequest = new URLRequest("assetsContainer.swf");
var loader:Loader = new Loader();
var myMCList:MovieClip = new MovieClip();
loader.contentLoaderInfo.addEventListener(Progress Event.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, loadComplete);

function loadProgress(event:ProgressEvent):void {
var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
percentLoaded = Math.round(percentLoaded * 100);
trace("Loading Assets: "+percentLoaded+"%");
}
function loadComplete(event:Event):void {
myMCList= event.currentTarget.content;
trace("Assets Loaded");
gotoAndStop("start")
}

loader.load(request);

Now, if want to populate a container with movie clips from my loaded swf I use this code:

var myMc:Object = new Object();
var container:MovieClip= new MovieClip();
myMc = myMCList.getChildByName(the name of my movie clip inside the loaded swf );
this.container.addChild(DisplayObject(myMc));
this.addChild(container);

So, I have all my assets in memory and I can add them to stage when I need.

What do you think? Makes sense?

costa
May 31st, 2007, 04:07 AM
Well… after testing deeper the code it seems I am not duplicating the movie clips, just using the current mc inside the loaded file. If I try to attach the same mc more than one time, flash gives me an error.
According to Adobe documentation:

"If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container "

Do you now how to duplicate an object?

eudora
May 31st, 2007, 10:07 AM
You can either create a new loader to load the same url or try using deep copy method found in the tips thread in this forum. :)

Do correct me if I am wrong..

costa
May 31st, 2007, 05:28 PM
I am quitting the idea to load a swf file with all my movie clips. It seems is not clear how to duplicate objects. So, I will put my mc in some frame in my main movie. Could you recommend a good tutorial for that? I tried this, but I did not work (my fault I guess)

http://flashas3.flashscript.biz/stage_preloader/Stage_preloader.html

Thanks

devonair
May 31st, 2007, 11:58 PM
it's actually fairly easy. here's a quick script I just copied and pasted (with a couple changes) from an earlier thread on the same subject - http://www.kirupa.com/forum/showthread.php?t=259632

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLET E, onAssetsLoad);
l.load(new URLRequest("assetContainer.swf"));

var MyLoadedClass:Class;

function onAssetsLoad(e:Event):void {
trace ("assets loaded");
l.contentLoaderInfo.removeEventListener(Event.COMP LETE, onAssetsLoad);
MyLoadedClass = e.target.applicationDomain.getDefinition("MyMovieClipClass");
stage.addEventListener(MouseEvent.MOUSE_DOWN, addToStage);
}

// duplicate loaded asset every time you click the stage..
function addToStage(me:MouseEvent):void {
var my_mc:MovieClip = new MyLoadedClass();
my_mc.x = Math.random() * stage.stageWidth;
my_mc.y = Math.random() * stage.stageHeight;
addChild(my_mc);
}

costa
June 1st, 2007, 12:47 AM
Thanks devonair, testing now!!!!