PDA

View Full Version : loader class basic question


savager2
02-10-2008, 02:46 AM
Once again, i'm just looking for the basic logic here..

i've got:
var _bgSWFholder:MovieClip = new MovieClip();
var _mainLoader:Loader = new Loader();
var _intro:URLRequest = new URLRequest('Assets/Intro.swf');
_mainLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, mainLoaderLoaded);
_mainLoader.contentLoaderInfo.addEventListener(Eve nt.ACTIVATE, mainLoaderActivated);
_mainLoader.contentLoaderInfo.addEventListener(Pro gressEvent.PROGRESS, mainLoaderProgress);
_mainLoader.load(_intro);


private function mainLoaderProgress(evt:ProgressEvent):void
{
var _percentLoaded:Number = evt.bytesLoaded/evt.bytesTotal;
_percentLoaded = Math.round(_percentLoaded * 100);
trace(_percentLoaded);
}

private function mainLoaderLoaded(evt:Event):void
{
trace('Load Complete');
_bgSWFholder = evt.target.content;
addChildAt(_bgSWFholder, 0);

}

private function mainLoaderActivated(evt:Event):void
{
trace('activated');
}



ok.. questions..

do i need to add _mainloader to the display list? with addChild(_mainLoader);

and more importantly, just the logic behind how this works.. ie: do i need to create a new loader, and event listeners for every item I want to load.. or can i just put _mainLoader.load(urlrequest); on let's say a button, and it'll load the new file..

as you can see.. the onLoadComplete function puts the loaded file inside a movieclip called _bgSWFholder. but it also addChilds _bgSWFholder as well..
so if I try to use _mainLoader.load on another swf, once it's done it's going to try to add _bgSWFholder again.. so maybe I shouldn't add _bgSWFholder in my complete function?

basically I've got it loading one swf into the holder.. everything is working.. but I need some guidance as to proper ways to be able to load a new swf into that holder when a button is pressed..




I've also looked into BulkLoader class.. and that seems really nice, but i'm kind of at the same question on that one, when you want to load something new in.. do i have to create another loader everytime.. ie: bulkLoader.add("main");
and then you start it..
bulkLoader.start();

but if on a button press I bulkLoader.add('second'); and start it again.. is that going to start loading main again as well..

I appreciate any help.. I'm not an actionscripter, and don't ever plan to be.. but if i can get my photo site up once then I can just update it, and I won't have to go through all of this again.. not that actionscript 3 is bad.. but i just see how much you can do with it.. and it's overwhelming..

I searched these forums and read like 30 posts about loader, but no where can i find how to load a different swf into the same movieclip holder when a button is pressed.. or how to deal with creating many of them..

thanks

sgartner
02-10-2008, 07:36 AM
Once again, i'm just looking for the basic logic here..

i've got:
<snipped, answers are general>

ok.. questions..

do i need to add _mainloader to the display list? with addChild(_mainLoader);

Well, you have a choice, you can add the loader to the display list since it will make the loaded object its child, but you probably wouldn't want to both add the loader and the loaded object to the display list.

and more importantly, just the logic behind how this works.. ie: do i need to create a new loader, and event listeners for every item I want to load.. or can i just put _mainLoader.load(urlrequest); on let's say a button, and it'll load the new file..

You don't need to create a new loader. The loader will load as many objects as you like, but you do need to be certain that the previous load is done as a new load will kill any current asynchronous load process. Also, the newly loaded object will replace the current object in the loader (a loader can only have one child).

as you can see.. the onLoadComplete function puts the loaded file inside a movieclip called _bgSWFholder. but it also addChilds _bgSWFholder as well..
so if I try to use _mainLoader.load on another swf, once it's done it's going to try to add _bgSWFholder again.. so maybe I shouldn't add _bgSWFholder in my complete function?

Well it is certainly true that if you re-use this loader to load another object the new object will eventually overwrite the previous in _bgSWFholder, but the previously loaded object will still be in the display list from the addChild() in the posted event handler. The bigger question is, "why are you putting the loaded object in _bgSWFholder at all?" It is certainly unnecessary in the code that you posted, but maybe there is code that requires it that you didn't post. If you never use it again, then using a global like this will just confuse things later (IMO).

basically I've got it loading one swf into the holder.. everything is working.. but I need some guidance as to proper ways to be able to load a new swf into that holder when a button is pressed..

If you will be loading a new SWF that will replace the previous, that is a clear reason to instead add the loader object to the display list since when you load the new object using load() it will then not only replace the object in the loader but also, by proxy, in the display list and you might be able to get rid of the Event.COMPLETE event handler entirely.

I've also looked into BulkLoader class.. and that seems really nice, but i'm kind of at the same question on that one, when you want to load something new in.. do i have to create another loader everytime.. ie: bulkLoader.add("main");
and then you start it..
bulkLoader.start();

but if on a button press I bulkLoader.add('second'); and start it again.. is that going to start loading main again as well..

I appreciate any help.. I'm not an actionscripter, and don't ever plan to be.. but if i can get my photo site up once then I can just update it, and I won't have to go through all of this again.. not that actionscript 3 is bad.. but i just see how much you can do with it.. and it's overwhelming..

Too late for that, you've written ActionScript and are now branded an actionscripter. You're lost, just accept it. Once you get your site up you'll just find a new use for it and start scripting again. There is no cure... HAHAHAHAHAHA

I searched these forums and read like 30 posts about loader, but no where can i find how to load a different swf into the same movieclip holder when a button is pressed.. or how to deal with creating many of them..

Well, hopefully this cleared up your questions...

savager2
02-10-2008, 08:49 AM
Well, you have a choice, you can add the loader to the display list since it will make the loaded object its child, but you probably wouldn't want to both add the loader and the loaded object to the display list.

ok.. so the content I'm loading is the child of the loader object.. thats good to know.. does that mean that it is loader.target..

You don't need to create a new loader. The loader will load as many objects as you like, but you do need to be certain that the previous load is done as a new load will kill any current asynchronous load process.

this is what I thought.. in my attempt here, i'm assuming it wouldn't matter that it kills the load if it's not complete, since it would only kill it trying to load a new swf into the same area.. which is fine as long as the new swf loads.. I'll just be sure to create new loaders for anything not loading into that same area

(a loader can only have one child).

i should have known this.. so that explains why onloadcomplete everyone kind of dishes out their content..

The bigger question is, "why are you putting the loaded object in _bgSWFholder at all?" It is certainly unnecessary in the code that you posted, but maybe there is code that requires it that you didn't post. If you never use it again, then using a global like this will just confuse things later (IMO).

definitely true.. But you're right, I'm loading it into _bgSWFholder because bgSWFholder is resized dynamically with the stage, and keeping the content inside proprtionate and filling the screen.. this method is me updating my as2 code.. maybe there's a better way now..

perhaps I can add _bgSWFholder to the stage before anything is in it.. and then onLoadComplete keep with my feeding the loaded content into it, but not add it to the stage.. since it's already there..

Too late for that, you've written ActionScript and are now branded an actionscripter. You're lost, just accept it. Once you get your site up you'll just find a new use for it and start scripting again. There is no cure... HAHAHAHAHAHA

haha.. i've been doing nothing but learning actionscript for the last month or two.. it's incredible what it can do, and if I continue learning and begin writing my own classes, i might have a problem.

you've already answered so much, thanks a ton.

savager2
02-10-2008, 09:03 AM
i've got what I was aiming for working.. I'm not sure if it's optimal, but I set it up so that _bgSWFholder is added when the entire thing is added to stage.. instead of when onloadcomplete fires.. and then I addChild my loader at 0, onload complete..

it doesn't work if i don't addchild my loader.. onloadcomplete..

but as of right now it seems to be doing what I want..

private function mainLoaderLoaded(evt:Event):void
{
trace('Load Complete');
_bgSWFholder = evt.target.content;
addChildAt(_mainLoader, 0);
rePosition();
}

sgartner
02-10-2008, 09:30 AM
i've got what I was aiming for working.. I'm not sure if it's optimal, but I set it up so that _bgSWFholder is added when the entire thing is added to stage.. instead of when onloadcomplete fires.. and then I addChild my loader at 0, onload complete..

it doesn't work if i don't addchild my loader.. onloadcomplete..

but as of right now it seems to be doing what I want..

private function mainLoaderLoaded(evt:Event):void
{
trace('Load Complete');
_bgSWFholder = evt.target.content;
addChildAt(_mainLoader, 0);
rePosition();
}

It looks like you answered your own questions from the previous message, so I won't bother. However, I see no reason why you should have to wait until the COMPLETE handler to add the loader to the display list:


var _bgSWFholder:MovieClip; // Will hold loaded movie clip
var _mainLoader:URLLoader = new URLLoader();
_mainLoader.load(new URLRequest("Assets/Intro.swf"));

function onMainLoaderLoaded (evt:Event) : void
{
trace('Load Complete');
_bgSWFholder = MovieClip(evt.target.content);
}

_mainLoader.addEventListener(Event.COMPLETE, onMainLoaderLoaded);
addChildAt(_mainLoader, 0); // no need to wait until COMPLETE

savager2
02-10-2008, 09:32 AM
i lied.. i just had to addChildAt(_mainLoader, 0) anytime in the beginning.. not onLoadComplete.. if there's a better way to keep it on bottom that i'm missing, let me know.. thanks

savager2
02-10-2008, 09:33 AM
haha.. you're fast.. thanks.. i can be taught

sgartner
02-10-2008, 09:35 AM
haha.. you're fast.. thanks.. i can be taught

Happy to help. I'm going to bed now, so you're on your own ;)

savager2
02-10-2008, 10:02 AM
I also realized, now of course, that i could have just made a variable loader that is equal to _mainloader, and then alter that outside of the method instead of bgSWFholder..