Jump to content
Search Community

Loading the actual SWF

kranke test
Moderator Tag

Recommended Posts

HI

 

I successfully implementing your awesome loader in my app.

 

I have a swf with a LoaderMAx instance and this loads another SWF which has a nested LoaderMAx.

 

So far I am able to track the loaded swf and the images inside that swf as a whole, but what I cannot figure out is how to actually include the loading time for the actual swf in which the main instance of the LoaderMax is created.

 

I hope I explained it well.

 

Thanks

 

Fernando.

Link to comment
Share on other sites

Hey Thanks for replying so soon.

 

Here is what I mean

 

MY setup

 

Main.swf (this has a loadermax instance mainQueue)

 

Content.swf (this is the SWF I load)

--- Inside this SWF I have another loadermax instance (which loads images from an array) imageQueue which I append to mainQueue)

 

So far I am able to track the total load (Content.swf + images) which is wonderful... But I will also like to add if possible the weight size of Main.swf and the time it takes to download).

 

Does it make sense?

 

Thanks!!!

Link to comment
Share on other sites

Once your SWFLoader fires its INIT event, that means it has loaded enough to look for loaders in the swf and begin loading them (if any are found). You can get any loader by name or url using LoaderMax.getLoader(). The latest version of LoaderMax (updated today) has a new "loadTime" property for all loaders which tells you how many seconds it took to load. So let's say you have a loader named "childLoader" inside your child swf that the SWFLoader is loading - you could get that like this:

 

var loader:SWFLoader = new SWFLoader("child.swf", {onInit:initHandler, onComplete:completeHandler});

function initHandler(event:LoaderEvent):void {
   var childLoader:LoaderMax = LoaderMax.getLoader("childLoader") as LoaderMax;
   trace("found " + childLoader);
}

function completeHandler(event:LoaderEvent):void {
   var childLoader:LoaderMax = LoaderMax.getLoader("childLoader") as LoaderMax;
   trace("childLoader took " + childLoader.loadTime + " seconds to load " + childLoader.bytesTotal + " bytes.");
}

loader.load();

 

Does that answer your question?

Link to comment
Share on other sites

Thanks for this explanation, this answers a different question but the original one still stands.

 

I already got what I needed from the SWFLoader and its nested loaders.

 

What I need is actually a way to add some sort of preloading to the main swf in which the SWFLoader is being created with the purpose of loading the content.

 

so

 

I have

 

main.swf (I create SWFLoader here to load "content.swf")

 

content.swf (I create LoaderMax to load the images)

 

I can get information with loaders for content.swf plus the LoaderMax queue with all images which is great but I also want to add to this the loading time for the actual main.swf (which is the initial swf)

Link to comment
Share on other sites

Sorry, obviously I've been misunderstanding the question. SWFLoader will automatically integrate the progress information about all the subloaders into itself. So if your swf file is 1000 bytes, then when SWFLoader assesses this, its bytesTotal will be 1000 UNTIL the subloading swf fires its INIT event at which point SWFLoader will look for LoaderMax-related loaders that have their requireWithRoot set to that swf's root (as indicated in the docs). Let's say that subloading swf has 10 ImageLoaders that total another 5000 bytes. The SWFLoader's bytesTotal will now be 6000 (1000 from the swf file itself and 5000 worth of images represented by ImageLoaders). See what I mean? All the information is there for you and neatly handled by SWFLoader itself.

 

One problem: if you're using a progress display (like a bar or circle) that indicated the overall loading progress, you'd see it quickly go almost to 100% (not quite) as it loads the swf and then if it finds any other loaders inside that swf and incorporates them into its overall progress, the value will go down again (in this case, it'd go from 99% down to 20% when it finds out that 1000 bytes out of the new total of 6000 have loaded).

 

Solution: define an estimatedBytes on your SWFLoader that incorporates the subloads. In this case, setting estimatedBytes:6000 on the SWFLoader would eliminate that shooting forward and backward again because it will prioritize the estimatedBytes until it can absolutely verify the contents of the swf, after which point it will adjust its bytesTotal automatically.

 

So if you want to show progress, just do something like this:

 

var loader:SWFLoader = new SWFLoader("child.swf", {onProgress:progressHandler, estimatedBytes:6000});
loader.load();
function progressHandler(event:LoaderEvent):void {
   myProgressBar.scaleX = event.target.progress; 
}

 

Does that answer your question? (finally)

Link to comment
Share on other sites

Dear Jack!

 

Yes this makes sense, but I also want to add to this LoaderMax the actual main.swf which is loading the child.swf.

 

So far and thanks for your advice I can get the loading percentage of the child.swf + all images. I need to also count for the main.swf (which is the preloader swf in which I create the LoaderMax instance)

 

to better explain myself, if you look at your demos.

 

You have

 

LoaderMax_subload_parent.swf

 

LoaderMax_subload_child.swf

 

---- Images that load into this SWF.

 

In this example you obtain the loading time of LoaderMax_subload_child.swf and he images that load into it. However you do not account (or at least where I can see the loading bytes of LoaderMax_subload_parent.swf)...

 

Basically I want to add to my loadermax mainQueue the parent.swf bytes size and load time.

 

I hope this makes sense.

 

Thanks as always for a prompt response. You are the very best.

 

Fernando.

Link to comment
Share on other sites

I think I understand now.

 

It is widely considered a best practice to use a preloader.swf whose sole job is to load your main swf, so that preloader.swf would have nothing except LoaderMax code and a preloader graphic all of which would have to load before LoaderMax could even begin anyway. I debated creating a simple SelfLoader class that would allow you to track the loading progress of the current swf, but frankly it seemed a bit wasteful and potentially confusing to end users. Also, a SelfLoader wouldn't be able to have the full functionality of all the other loaders because you couldn't unload() it or cancel() or get an accurate loadTime, etc. See what I mean? So I'm having a hard time seeing the practical advantage of creating SelfLoader and I'd rather encourage folks to follow the best practice of using a simple preloader.swf - I assume you're not doing that, right? Or did I misunderstand again? :)

Link to comment
Share on other sites

I would like to request an example of using LoaderMax as a simple preloader as mentioned above.

 

I've tried to add a progress bar and on onComplete have the swf fade in. This seems to require setting the container to alpha 0 which if container:this you cant see it. I've tried to change that property and I keep getting errors. I've given it a shot and can't seem to make it work. Any examples would be very helpful. Thank you for your great work.

Link to comment
Share on other sites

It should be as simple as:

 

import com.greensock.TweenLite;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.SWFLoader;

var loader:SWFLoader = new SWFLoader("myFile.swf", {alpha:0, container:this, onProgress:progressHandler, onComplete:completeHandler});
loader.load();

function progressHandler(event:LoaderEvent):void {
   //code here that updates your progress bar, like:
   myProgressBar.scaleX = event.target.progress;
}

function completeHandler(event:LoaderEvent):void {
   TweenLite.to(event.target.content, 1, {alpha:1}); 
}

Link to comment
Share on other sites

Hi Jack,

Whilst I totally agree that your approach is the right one and 'best practice' I would like to see the addition of a SELFLoader.as class. Could this be an optional activation so that it doesn't add additional weight and leaves the default as the preferred 'best practice' external loaded?

 

I often find that restrictions placed on me by legacy infrastructures and clients demands mean that I have to adopt the less efficent 'single SWF preloading' approach. This is particulary the case with my main client who I make games for, as whilst they allow the loading of external assets the actual main part of the game need to be 'self sufficient'. Your loader class is great for getting these external assets in as I can be confident that when the queue has completed the asset will be there, that the xml will not have errors etc.

 

I guess it if farly simple to incorporate your loader and a bog standard one along these lines:

addEventListener(Event.ENTER_FRAME, mainLoadTracker);

private function mainLoadTracker(event:Event):void
	{
		MAINSWFLoadPercent = (((loaderInfo.bytesLoaded / loaderInfo.bytesTotal) + JACKSPercentage) / 2) * 100
		if (MAINSWFLoadPercent == 100 && isAssetsLoaded == true)
		{
			//game and all assets totally loaded
		}
	}

 

In the above code I am getting an acurate % load of the mainSWF as JACKSPercentage is updated by your onProgress:progressHandler. I also get the additional safty check that the assets are loaded as

isAssetsLoaded 

is only set to true when loaderMax completeHandler is fired by your class.

 

However is doesn't seem to be as neat as it could be. One suggestion would be to add a switch to a particular LoaderMax queue somewhere along the lines of SELFLoad=true. If this if detected the loader class would add its self to that queue much in the same way I have done manually.

 

or

	
var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
queue.append(new SELFLoad());

 

and have the weight of the mainSwf added to the queue.

 

I understand that both these approaches might cause problems and that my initial solution may actually be the simplest but I thought I'd see what your thoughts were.

 

Cheers

 

Matt

Link to comment
Share on other sites

Well, the problem with the solution you offered is that it doesn't accurately weight the swf and the other loaders. If your swf is 20k and the loaders in the LoaderMax represent 2000k, your percentage would be off because it gives equal weight to them both. You'd need to do (loaderInfo.bytesLoaded + queue.bytesLoaded) / (loaderInfo.bytesTotal / queue.bytesTotal) to get the correct progress.

 

But I went ahead and whipped together a SelfLoader class and attached it here. Is that what you were looking for? Just drop it into the com/greensock/loading package and you should be good-to-go.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...