Jump to content
Search Community

Partial preloading before play, then cancel to load new SWF

caffrey75 test
Moderator Tag

Recommended Posts

Hi there,

 

First off, thank you so much greensock guy (Jack) for your amazing creations. I only started using TweenLite/TweenMax the other day because I got so fed up of Flash's own limited Tween engine, then I moved on to coding a preloader and just couldn't believe my luck when I then discovered LoaderMax. You've saved me literally hours/days of time, enabling me to concentrate on stuff that I AM good at. I hope your stuff makes you a millionaire, because you definitely deserve it :)

 

Typically I've run into a problem with LoaderMax because of my own ineptitute. After 6 years of coding in AS2, I finally moved onto AS3 2 weeks ago and am just about getting to grips with it, but my coding has probably always been generally awful, although I try ;)

 

What I'm trying to do is preload part of a swf from the timeline of the main swf before playing. These swfs generally contain video and some other content (the video being on the main timeline of each, so that I can partially preload properly (I got some help from this thread viewtopic.php?f=6&t=5189 as a guy there was doing similar).

 

Problem I'm getting is that I've got two or three swfs that I want to load in this fashion (partial preload and then play), but they would load in and overwrite the swf already in place. Everything worked brilliantly until I discovered that, if I attempted to load another swf BEFORE the first one had loaded in full, not only was the first one still in-place (despite calling removeChild), but if I attempted to reset the _loader object I'd also get -

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.

 

I'm clearly doing something wrong, although I've tried various things and really attempted to find the solution on the boards/in the documentation before posting. The reason I haven't used the queueing options available to preload these swfs is probably a combination of being a tad nervous of it, and basically wanting to learn to walk before flying. Here's my commented code. Sorry for my waffle, but I try to explain problem as fully as possible, and apologies for my messy code hehe

 

var loadedSWF:MovieClip;
//declare the loadedSWF 'holder' outside of the function

function loadPage() {
//function to load a new page on a button click.
showLoader(); 
//shows a loading anim
var minPercentLoaded:Number=pagesArray[pageToLoad].minPercentToLoad/100;
//the amount I want to load before playing (taken from an array of page URLs and other info)
if (loadedSWF!=null) {
//my attempts to reset after checking that there IS another swf currently playing/being loaded or both
	_loader.dispose(true); 
//not sure about this, been trying various things (pause, cancel etc) to try and avert problem.

	maincontentMC.contentholderMC.loaderMC.removeChild(loadedSWF);
//attempt to remove the previous swf from its location. Successful but only if the swf has been loaded in full.

	loadedSWF=null;
//sets the loaded swf back to null
}
var _loader:SWFLoader = new SWFLoader( pagesArray[pageToLoad].file, {auditSize:false, autoPlay:false, onProgress:_onLoaderProgress } );
//loads a file based on its position in the pagesArray.
_loader.load();
function _onLoaderProgress(event : Event):void {
//not too sure if I should have this nested in the load() function, but it seems to work fine when not simulating download.
	preloader.textMC.percentText.text= (Math.ceil(((event.target.progress)*100)*pagesArray[pageToLoad].minPercentToLoad).toString())+"%";
//sets the progress display in the preloader animation textfield.
	if (loadedSWF == null && _loader.progress >= minPercentLoaded) {
//checks that loadedSWF is null and minimum amount has loaded before playing
		loadedSWF =_loader.rawContent as MovieClip;
		if (loadedSWF) {
			maincontentMC.contentholderMC.loaderMC.addChild(loadedSWF);
//adds the loadedSWF into the correct place
			hideLoader();//hides the loading animation
			loadedSWF.x=(~(pagesArray[pageToLoad].dim[0]/2)+1);
			loadedSWF.y=(~(pagesArray[pageToLoad].dim[1]/2)+1);
//sets the correct x and y values
			openMask();
//reveals the loaded swf (the openMask function also orders it to play when done.
		}
	}
}
}

Link to comment
Share on other sites

I've read this post like 4 times and I'm having a tough time understanding how it works or how it is supposed to work. It's very difficult to visualize this scenario without seeing the interactions that are supposed to trigger the loading of the various swfs.

 

The code is a bit difficult to read. It appears as though you are using the same loader to load multiple swfs and conflicts are arising when you try to load more than one swf at a time.

 

Once the swf is partially loaded, what happens when it gets played? does the download resume automatically? or do you have something in place to trigger that?

 

My gut tells me that you are probably better off having a SWFLoader for each swf and it will be much easier to monitor the individual load progress. I'm also not experienced with only loading part of a swf.

 

I know you admitted that you are new to this, and that is fine, so am I. I just wonder if wouldn't be easier to explore some of the features of LoaderMax which allow you to monitor the loading and playback of multiple assets instead of trying to get this "one SWFLoader for all" approach to work.

 

I don't know how much help I am going to be to you, but my bet is that it would be much more helpful if you posted some files that could be tested. to make things easier the subloaded swfs don't have to be massive or have anything crazy going on.

 

perhaps a solution will be more clear to someone else.

Link to comment
Share on other sites

Yeah, I definitely wouldn't use nested functions like that - they're considered a poor practice in ActionScript (they can cause garbage collection problems and some other funky stuff).

 

Which line is generating that 1009 error? Have you tried adding trace() statements to see? Feel free to post a simple FLA that demonstrates the problem so that we can take a peek and publish for ourselves. I'm sure we can get it figured out.

 

Carl's suggestion not to reuse SWFLoader instances like that is a good one, particularly because in order to work around a bug in Flash, SWFLoader must keep loading until the first frame finishes loading before it can cancel it. Otherwise, Flash often gets stuck and can't release the subloaded swf for garbage collection. That's one of the benefits of using LoaderMax - it automatically works around a ton of those little bugs, inconsistencies, and annoyances. But if you're reusing a SWFLoader right away before it finished loading the first frame of that child swf, it would make sense that you'd run into trouble. I would definitely recommend using distinct SWFLoader instances.

Link to comment
Share on other sites

Thanks so much both of you by taking the time to reply at such length.

 

Sorry about the difficult-to-read-code, Carl - it's messy I know ;)

 

I'm basically building my own Portfolio website, but because I'm a one-man army I'm doing it all myself (involving Flash, 3D Max, After Effects) and sometimes it feels like I'm trying to do too much - there is SOOO much still to do! Hence I probably tried to cut corners by NOT using the queueing functionality LoaderMax provides and prioritising each movie. I will now attempt to do it this way.

 

You're right, Carl, I was using the same loader to load multiple swfs - here is the typical scenario.

 

1. User enters the site (which loads up using a SelfLoader)

 

2. Once SelfLoader has completed a SWFLoader is initiated which begins loading the first item in the Array by default (which is intro.swf)

 

3. intro.swf is a rolling video embedded in the main timeline with sound etc. It's about 3.5MB big and HAS to start playing before it's completely cached in. Kind of like a YouTube video.

 

4. User is obviously able to skip the intro and choose another of 3 options, which would all load in another page and the intro would be unloaded if this happens (about.swf, porfolio.swf, contact.swf)

 

5. THE PROBLEM:

 

if (loadedSWF!=null) {
	trace ("1");
	_loader.cancel(); //problem lies here whether I use dispose(true), cancel or whatever and throws the 1009 error.  
	trace ("2");
	maincontentMC.contentholderMC.loaderMC.removeChild(loadedSWF);
	trace ("3");
	loadedSWF=null;
	trace ("4");
}
var _loader:SWFLoader = new SWFLoader( pagesArray[pageToLoad].file, {auditSize:false, autoPlay:false, onProgress:_onLoaderProgress } );
trace ("5");

 

If I comment out _loader.cancel() and allow intro.swf to fully complete its load before trying to load anything else instead, then it will work. However, if I try to load something else in BEFORE intro.swf finished loading in full then (provided _loader.cancel isn't present) I won't the 1009 error, but intro.swf does not get removed and is still there playing, even with removeChild() (probably cos the SWFLoader hasn't finished with it), although the first frame has obviously been loaded.

 

Thanks for offering to take a look at my Flash, guys, I might post a file up if I still experience problems after trying to do it the way you both suggested.

 

The only thing of paramount importance is that intro.swf (and any other of the three other swfs) are ordered to play after a certain percentage of the file has been loaded in but only (with the exception of intro.swf) after a user has opted to view them. Just got to work out how to effect this in the code, so I think I'll be using the prioritise functionality of LoaderMax. I will also make sure the function isn't nested, Jack ;)

 

Thanks again guys, I don't know if the above has been a bit more enlightening as in what I'm trying to do. I'm not one of these that would ever say 'Pleeeease do it for me' because it's more rewarding to work it out myself, but if you have any further helpful comments or glaring errors to point out in my theory, please write.

 

 

EDIT: I just realised while setting up the new system why the intro.swf DOESN'T get removed when using remove child. Immediately after it gets removed and loadedSWF gets set to null, the requirements of the if statement below are fulfilled and it just gets added again immediately and the whole thing breaks! *slaps forehead*. I think there are still glitches though, so working on setting LoaderMax more like you guys suggested though to avoid further mishaps :)

 

if (loadedSWF == null && _loader.progress >= minPercentLoaded) {
		loadedSWF =_loader.rawContent as MovieClip;

		if (loadedSWF && !introLoaded) {
			introLoaded=true;
			maincontentMC.contentholderMC.loaderMC.addChild(loadedSWF);

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