a-ok Posted December 19, 2010 Share Posted December 19, 2010 I'm new to AS3 and LoaderMax and I don't know how to access the loaded swf. function loadthisnow(loadswf:String):void { var contentloader:LoaderMax = new LoaderMax({name:"contentLoader",onProgress:progressHandler,onComplete:completeHandler,onError:errorHandler}); contentloader.append(new SWFLoader(loadswf,{name:loadswf,estimatedBytes:1024*1024,container:this.container})); contentloader.load(); } And the loaded swf appears but how do I do stuff to it? I can do stuff to this.container but how do I access the loaded swf? I ask this because when I load other content it just appears in front of the previously loaded content so I want to be able to delete the previously loaded swf or move it or something. There is a blurb in the documentation about empty, dispose or something but it's too short and doesn't have examples. Thanks. Link to comment Share on other sites More sharing options...
GreenSock Posted December 19, 2010 Share Posted December 19, 2010 You can access the raw content of the SWFLoader with its "rawContent" property. Like this: function loadthisnow(loadswf:String):void { var loader:SWFLoader new SWFLoader(loadswf, {estimatedBytes:1024*1024, container:this.container, onComplete:completeHandler}); loader.load(); } function completeHandler(event:LoaderEvent):void { var mc:MovieClip = event.target.rawContent; trace("rawContent: "+mc); } Notice that you don't have to put the SWFLoader into a LoaderMax instance if you don't want to - it's totally optional. The only benefit of putting it into a LoaderMax is if you have more than one and want to control or report their progress as a whole. If you want to dump the SWFLoader and its content, just call dispose(true) on that instance: loader.dispose(true); And you can get the loader anytime with LoaderMax.getLoader() - just pass the url or name like this: var loader:SWFLoader = LoaderMax.getLoader("http://www.greensock.com/swf/child.swf"); loader.dispose(true); Have you watched the videos at http://www.greensock.com/loadermax-tips/ Link to comment Share on other sites More sharing options...
a-ok Posted December 20, 2010 Author Share Posted December 20, 2010 Now I really don't understand what's going on. Your example doesn't work for me - nothing happens. I want to load a swf INTO a movie clip called container that is already on the timeline. In doing so I want to replace whatever other content that movie clip has. When I load something else into that movie clip, I want it to replace the previous content. var loader:SWFLoader = new SWFLoader(loadswf, {estimatedBytes:1024*1024, container:this.container, onComplete:completeHandler}); loader.load(); function completeHandler(event:LoaderEvent):void { container = event.target.rawContent; // this is probably what's wrong but I don't know how to put a loaded clip into an existing clip on the timeline TweenLite.to(container, 1, {alpha: 1}); } This is so annoying, in AS2 I can do everything I want but in AS3 I can't even get my head around the simplest things. Some progress... Link to comment Share on other sites More sharing options...
GreenSock Posted December 21, 2010 Share Posted December 21, 2010 Did you watch the videos? Just curious. It's kinda tough to troubleshoot without seeing the problem in context, but here are some thoughts: 1) If you want to remove all the content inside a MovieClip when you load a new swf, you'd need to either dump the whole MovieClip and just recreate it or go through all of its children and removeChild() each one like this: var i:int = mc.numChildren; while (--i > -1) { mc.removeChildAt(i); } 2) When you define a "container" in your vars parameter of your SWFLoader, you're already telling it to load into that object. For example: var loader:SWFLoader = new SWFLoader("child.swf", {container:myObject}); loader.load(); That will load child.swf into myObject. Does that help? Again, I'd highly recommend watching the videos and downloading the sample files that Rich Shupe provides. http://www.greensock.com/loadermax-video/ Link to comment Share on other sites More sharing options...
a-ok Posted December 22, 2010 Author Share Posted December 22, 2010 Did you watch the videos? Just curious. Yeah, I got to the half of the second video where he says "The display of loaded assets is simplified by optionally wrapping content in an class instance that extends a native display object." and then I turned it off because I don't know what that means. Besides I never learned anything by actually listening to someone, it would be nice to have a text tutorial. 1) If you want to remove all the content inside a MovieClip when you load a new swf, you'd need to either dump the whole MovieClip and just recreate it or go through all of its children and removeChild() each one like this: That worked, thanks. I'm used to AS2 where it's loadMovie(url,target) and when you do it again it replaces the previously loaded movie. LoaderMax adds a child to the container and I didn't know how to remove it. removeChild(), it's so obvious now. 2) When you define a "container" in your vars parameter of your SWFLoader, you're already telling it to load into that object. For example: Yeah but when I load something else into that same object it gets added in front of it instead of replacing it. Does that help? Yeah thanks, that's all I needed. Link to comment Share on other sites More sharing options...
a-ok Posted December 23, 2010 Author Share Posted December 23, 2010 Another little thing: I have a menu with several items, each of which loads one swf into a movieclip called container. However, if a menu item is clicked while a swf is loading then both SWFs start loading at the same time (twice as slow). I'm sure this is useful in many cases but I'd like whatever was previously loading to be discarded. How do I do that? function loaderflow(e:Event):void { if (gvc.vars.loadthisnow!=loadswf) { loadswf = gvc.vars.loadthisnow; trace("Now loading "+loadswf); var i:int = this.container.numChildren; while (--i > -1) { this.container.removeChildAt(i);} var loader:SWFLoader = new SWFLoader(loadswf, {container:this.container, onComplete:completeHandler}); loader.load(); function completeHandler(event:LoaderEvent):void { TweenLite.to(container, 1, {alpha: 1}); } } } Thanks Link to comment Share on other sites More sharing options...
GreenSock Posted December 23, 2010 Share Posted December 23, 2010 You can call unload() on a SWFLoader (or any loader) anytime. But please note that there are some bugs in Flash that prevent proper garbage collection if you try stopping a swf load before its first frame has initted, so SWFLoader automatically works around this problem internally by allowing the swf to keep loading until it can properly stop/cancel/unload it. This is transparent to you, but I just wanted to point it out in case you're wondering why you don't see the bandwidth immediately change when you unload() or cancel() a SWFLoader that's in progress. You could cance() an in-progress loader, but If your goal is to get rid of the content I'd just use unload() because that'll automatically cancel the load anyway. If you happen to cancel() a SWFLoader that already completed its loading, it'll have no effect (which kinda makes sense if you think about it). In summary, unload() your previous/old SWFLoader whenever you want. In fact, if you want to dispose of the loader itself as well, then call dispose(true) which will dump the content and nuke the loader, making it eligible for garbage collection. Does that answer your question? Link to comment Share on other sites More sharing options...
a-ok Posted December 23, 2010 Author Share Posted December 23, 2010 You can call unload() on a SWFLoader (or any loader) anytime. Yes, it's ok now, thanks. I read about methods unload() and dispose() but due to my poor grasp of AS3 basics I didn't know how / where to use it. So much about learning as I go... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now