Jump to content
Search Community

Double Loading ?

sandesh test
Moderator Tag

Recommended Posts

Hi Jack,

 

I am loading a swf using LoaderMax's SWFLoader which is appended to a loaderMax instance as usual.

This code is inside a function. Now what i want to ask is when this funtion runs for the first time, it will load the swf.

 

But if the function is run again, will it start loading the swf again or just display the loaded swf ?

Link to comment
Share on other sites

It depends how you wrote your code. Let me explain...

 

If your function creates a new SWFLoader instance each time it runs, then yes it will load the swf again (two copies of the swf will be in memory). This can actually be a good thing for cases when you want to, for example, show two instances of a swf on the stage. But if you create a single SWFLoader and then invoke load() more than once, it will NOT keep reloading. It keeps track of its state, so after it has completely loaded, calling load() again will have no effect (other than immediately dispatching the COMPLETE event). The same goes for loaders that are inside a LoaderMax - if you load() the LoaderMax queue many times, it will only load loaders that haven't loaded yet.

 

Even if you create multiple SWFLoader instances and load() them, keep in mind that typically the browser's cache is used so the first one will take the longest to load and then subsequent loads will draw from the cache very quickly.

 

Does that answer your question?

Link to comment
Share on other sites

Hi Jack,

 

I agree to what you said, but it confuses me where multiple swfs are being loaded. Mentioned below is what i want.

 

 

GOAL:

1) 3 different buttons on stage. On clicking each will load a new swf.

2) If a swf is already loading, it will remove the previous loading and then load the new swf (as per the button i clicked)

3) If a button is clicked a second time, then there will be no loading and just display the loaded swf

 

 

DOUBTS

While creating the above example, there were some problems that i faced and stopped midway.

While clicking a new button, I have to take care of 2 things (a) track if the swf is already loaded or not. If not I have load the swf (B) Dispose the previous loading if a new button is clicked

 

Regarding point (a), I have the doubt that I have to execute the load command to load the swf, which means I have to trigger the load command on the loadermax instance which contains 3 different swf loaders, but in this case i have to load just 1 swf, according to the button i clicked. So how will I just load or reload the one i want

 

Regarding point (B), for some reason the dispose/unload command does not seem to work for some reason, If i simulate the download in flash.

 

I hope you have understood my queries above. Thanks & waiting for ur reply

Link to comment
Share on other sites

How about this?:

 

var page1:SWFLoader = new SWFLoader("page1.swf");
var page2:SWFLoader = new SWFLoader("page2.swf");
var page3:SWFLoader = new SWFLoader("page3.swf");
var currentPage:SWFLoader;

button1.addEventListener(MouseEvent.CLICK, clickHandler);
button2.addEventListener(MouseEvent.CLICK, clickHandler);
button3.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void {
var next:SWFLoader;
if (event.currentTarget == button1) {
	next = page1;
} else if (event.currentTarget == button2) {
	next = page2;
} else {
	next = page3;
}
if (next == currentPage) {
	return;
}
if (currentPage != null) {
	removeChild(currentPage.content);
	currentPage.unload();
}
currentPage = next;
currentPage.load(true);
addChild(currentPage.content);
}

 

The tricky thing about your point #3 is that swfs are a unique beast in the sense that when you load a swf it will normally start playing immediately. Of course you can set autoPlay:false but if you have a bunch of nested MovieClips inside that swf or code that starts doing things on the first frame, there's no way to stop that really. So in that case it'd be best to simply reload it. Notice that I used load(true), not just load() because I wanted it to force a full reload (per your request). That would make it play from the beginning too.

Link to comment
Share on other sites

Hi Jack ....

 

I have tested your code according to my needs and there are a few things i noticed...

 

1) i suppose the last 2 lines should be

 

next.load(true);
addChild(next.content);

 

instead of

 

currentpage.load(true);
addChild(currentpage.content);

 

since currentpage gives me a null reference

 

 

2) i understand that by giving load(true), reloads the swf and doesn't need to control nested movieclips within the loaded swf.

But i noticed that it beats the purpose of loading and reloads again the whole file. Isn't there a way to control the movieclips and also load the swf only once.

 

3) The unload feature doesn't seem to work. It doesn't unloads the previous swf.

 

 

Pls check the attachment

Link to comment
Share on other sites

1) Sorry, it was very late and I was sleep-deprived - I forgot one line of code. I needed to add currentPage = next (remove the changes you made too). I edited the code in the above post to be correct.

 

2) Not really. Let's say you have ActionScript-driven tweens on the first frame of your swf and/or a bunch of other code that does stuff like begin sounds or videos. How would SWFLoader stop that? As far as I know, it's impossible. However, if you know you're dealing with swfs that don't have anything like that which would make it undesirable to load and have sitting in memory, you could definitely load it in and simply add it to the stage when you need it and gotoAndPlay(1) on it. But remember, once the swf is loaded once, it should be stored in the browser's cache, so subsequent loads would be much faster anyway.

 

3) The simple line of code that was missing (mentioned in #1) caused this problem. It had nothing to do with SWFLoader not being able to unload() - it was just that currentPage was always null, so the conditional code that would run the unload() never got invoked. Setting currentPage = next as indicated above should fix things and cause unload() to be called. Again, sorry for the confusion.

Link to comment
Share on other sites

Hi Jack

 

I have entered the new code and now i am not getting the null error.

But still, somehow i feel that the unloading thing is not happening properly

 

What i did was simulated the download to 56k in compiled version.

Then i clicked the buttons one after the other from top to bottom.

What i expected was when i click the 2nd button, it should remove the 1st loading swf from the Bandwidth profiler window and on clicking the 3rd button it should have removed the 2nd loading swf.

 

But its showing all the 3 swf loading alongwith its progress. I have attached the screenshot. Also I get a 'Load Never Completed' error for swfs that I interrupt (eg, page1.swf & page2.swf).

 

Am i doing something wrong? Pls help...

Link to comment
Share on other sites

There are two issues at play here:

 

1) There's a known bug in the bandwidth profiler in Flash that will make it act like there are still files streaming when there shouldn't be. You can add trace() statements inside the com.greensock.loading.core.DisplayObjectLoader class where unloadAndStop() is called on the loader (or unload() when publishing to Flash Player 9). You'll see that it is getting called.

 

2) A bug in Flash prevents swfs from being properly unloaded and garbage collected if you close() or unload() or unloadAndStop() the Loader that's loading it before the swf instantiates its first frame. Therefore, SWFLoader automatically implements internal logic to wait for that before it closes and unloads it. This is all transparent to you - you can unload() or cancel() the loader at any time - it automatically protects you from garbage collection hassles like that. But it obviously must allow the swf to continue loading until that swf's first frame renders. I did, however, notice one thing this morning that needed to be improved in SWFLoader in terms of closing the Loader in a particular situation, so make sure you download the latest version. http://www.LoaderMax.com

 

As I built LoaderMax, I ran into MANY bugs in Flash that often trip users up. This is another reason why you can have more confidence using LoaderMax compared to just Adobe's built-in stuff. It tries to protect you from the bugs, inconsistencies, and hassles.

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...