dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Unloading SWF from movieclip

  1. #1
    Junior Member
    Join Date
    Mar 2007
    Posts
    26

    Unloading SWF from movieclip

    Hi guys, i need help with this code that concerns unloading a SWF from a movie clip. This code is inside a movie clip acting as a button that loads the SWF into another movie clip in the main timeline called "rootbox_mc". Basically when i spam the button, it keeps loading the external SWF over and over the one already loaded, i want it to remove the one already loaded before loading another in its place.

    More specifically, i wanted to ask why my if statements were not working, and how do you actually unload a loaded external SWF. I read that you just delete the child, and that's what i tried to do here.

    Code:
    this.addEventListener(MouseEvent.CLICK, load1Release);
    	function load1Release(evt:MouseEvent):void {
    
    		var checker:Number=0;
    		var myLoader1:Loader = new Loader();
    		
    		if(checker==1) {
    			removeChild(myLoader1);
    			//myLoader1.unload;
    			addChild(myLoader1);
    		} else {
    			addChild(myLoader1);
    			var myRequest1:URLRequest = new URLRequest("maya_import.swf");
    			myLoader1.load(myRequest1);
    			
    			// ---------- progress tracking AS ----------
    			
    			myLoader1.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader1);
    			myLoader1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress1);
    			myLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult1);
    			
    			function showPreloader1(evt:Event):void {} 
    			
    			function showProgress1(evt:ProgressEvent):void { 
    				var loadProgress_percent = evt.bytesLoaded / evt.bytesTotal; trace("loadProgress_percent: " + loadProgress_percent*100);
    			} 
    			
    			function showLoadResult1(evt:Event):void {
    				MovieClip(parent).rootbox_mc.addChild(myLoader1.content);
    				checker=1;
    				trace(checker)
    				//trace("GOT showLoadResult");
    				//trace("maya_import LOADED!");
    				//rootbox_mc.stop();
    			}
    		}
    	};
    Thanks!

    ps; This code is someone else's code that i found on the internet. I tried to modify it to fit what i'm trying to do, but it is currently not working. I am a total flash newb so i don't understand all of it.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The main problem you have is that your checker variable is defined locally in your load1Release function and so will ALWAYS be 0 at the if.

    The main secondary problem you have is that you've defined showPreloader1, showProgress1, and showLoadResult1 inside load1Release, so they are scoped as local to load1Release and each time you execute it, you get new instances of those.

    A third issue is that you are adding the loader content to the parent after adding the loader itself. When you remove the loader content from the loader, you cannot call unload anymore because it throws an error trying to remove its non-existant child.

    You should also not use ints/numbers as boolean conditions, that's why there's Boolean.


    Code:
    var checker:Boolean = false;
    var myLoader1:Loader = new Loader();
    addChild(myLoader1);
    // ---------- progress tracking AS ----------
    myLoader1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress1);
    myLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult1);
    
    function showProgress1(evt:ProgressEvent):void { 
    	var loadProgress_percent = evt.bytesLoaded / evt.bytesTotal;
     	trace("loadProgress_percent: " + loadProgress_percent*100);
    } 
    		
    function showLoadResult1(evt:Event):void {
    	checker=true;
    	trace(checker)
    }
    
    this.addEventListener(MouseEvent.CLICK, load1Release);
    function load1Release(evt:MouseEvent):void {
    	if(checker) {
    		myLoader1.unload();
    		//here you may want to use unloadAndStop() if you're using flash 10
    		//and you might want to kick off any subsequent loads.
    	} else {
    		var myRequest1:URLRequest = new URLRequest("maya_import.swf");
    		myLoader1.load(myRequest1);
    	}
    };

  3. #3
    Junior Member
    Join Date
    Mar 2007
    Posts
    26
    Woah, thanks dude! That was fast as, as expected there are heaps of problems because i'm a total newb to flash, and more so now with the coming of AS3.

    I'm in the process of looking over it to understand it. Just a quick question, i can't seem to find my rootbox_mc in there, so how is it meant to stick the external SWF into this movie clip that is on the main timeline?

    Thanks again!

  4. #4
    Junior Member
    Join Date
    Mar 2007
    Posts
    26
    I just tested it out then, without the rootbox_mc, the loading of the external SWF results in it loading into some obscure place. I'm not quite sure how to modify your code again so that it would load into my rootbox_mc on the main stage.

    Actually the point of putting the if statements in, was that i had multiple buttons (movie clips) which this code was located in (was targetting with 'this.') with the SWFs changed, so that i could click on different buttons, and it'll unload the current one, before loading the new one.

    The point of the unloading checking for itself, was so it wouldn't stack upon itself if someone pressed the button many times.

    Thanks.

    ps; i've attached my testing file if it will help. Thanks for taking the time to help :-D
    Attached Files Attached Files
    Last edited by senkei; 06-02-2009 at 01:34 PM.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's loading into the Loader. Which was not positioned. You see "addChild(myLoader1)"? If you instead put
    Code:
    MovieClip(parent).rootbox_mc.addChild(myLoader1);
    It will become a child of the rootbox_mc as you were trying to do with the content afterward. Be aware that this is slightly different and if the code in the loaded swf refers to parent it won't work the same (because parent will be the Loader, not the rootbox_mc).

    Instead of duplicating this code on a bunch of movieclips, it would be better to abstract out the common parts (probably everything other than the URL, right?) and use the same function over again. If you put the URL as a property on those movieclips, you can get it back in load1Release by using the currentTarget property of the event.

    Code:
    function load1Release(evt:MouseEvent):void {
    	var mc:MovieClip = MovieClip(evt.currentTarget);
    	if(checker) {
    		myLoader1.unload();
    		//here you may want to use unloadAndStop() if you're using flash 10
    		//and you might want to kick off any subsequent loads.
    	}
    	var myRequest1:URLRequest = new URLRequest(mc.urlToLoad);
    	myLoader1.load(myRequest1);
    };
    This assumes you've assigned the url to the property "urlToLoad" of the clip to be clicked. I also moved stuff out of the "else" because you want to load it regardless. Only the unload is conditional.

  6. #6
    Junior Member
    Join Date
    Mar 2007
    Posts
    26
    Thanks for the help 5Tons! While i was waiting for your reply i actually tried a different method of loading multiple SWFs files and it seems to work, even though it is a little unconventional.

    However i ran into a couple of problems i was hoping you could help me with.

    It concerns the Home button (home_btn) and the "book1" and "book2" on the timeline. Basically home_btn works everywhere on the timeline except for when we are on the book1/2 frames. At which point pressing home_btn only repeats the trace on book1/2 frames. I suspect it has something to do with the if statements, but i have no idea how to fix it.

    While you are there, could you suggest a way i could unload the loaded SWFs? I suspect it might become a problem later on (basically i'm going to load external SWFs on the book1/2 frames).

    Thanks for all your help, i'm in a little pinch for leaving things to last minute :-(
    Attached Files Attached Files

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then in a pinch you will remain. I can't open .flas here as I don't have flash installed at work, and your description hasn't given me enough to go on.

    As for unloading swfs, if they are still in the Loaders you can use the unload method. If you take them out of the Loaders I would suggest using this process:
    Code:
    var m:MovieClip = MovieClip(loader.content);
    loader.unload();
    something.addChild(m);
    That will remove the reference to the movie in the loader so you don't have those hanging around eating memory. You'll still have to remove any other references and remove the clips from the stage to make them eligible for garbage collection.

  8. #8
    Junior Member
    Join Date
    Mar 2007
    Posts
    26
    I guess i'll look into that while waiting on this (thanks).

    Basically on my timeline, frame 1 has the name "start" and has the following code:

    Code:
    stop();
    
    main_txt.text = "Welcome to Borders";
    sub_txt.text = "Pick and option below to begin";
    
    var bookNumber:Number=0;
    
    browse_btn.addEventListener(MouseEvent.CLICK, browseRelease) 
    	function browseRelease(evt:MouseEvent):void {
    		play();
    	};
    
    home_btn.addEventListener(MouseEvent.CLICK, homeRelease) 
    	function homeRelease(evt:MouseEvent):void {
    		gotoAndStop("start");
    	};
    The button in question is the last one (home_btn).

    On frame 30 "direction" we have:

    Code:
    stop();
    
    this.addEventListener(Event.ENTER_FRAME, whichBook);
    	function whichBook(evt:Event):void {
    
    		if(bookNumber == 1) {
    			gotoAndPlay("book1");
    		} else if(bookNumber == 2) {
    			gotoAndPlay("book2");
    		} else {
    			gotoAndStop("start");
    		}
    };
    
    //trace(bookNumber);
    That is the loop in question, and following it on frames 40 and 60 are "book1" and "book2" respectively.

    With codes (1, 2, in this order):

    Code:
    stop();
    
    trace("you are at book1");
    
    /* var loader:Loader = new Loader();
    addChild(loader);
    var myRequest:URLRequest = new URLRequest("maya_import.swf");
    loader.load(myRequest);
    swf_mc.addChild(loader.content);
    
    */
    
    bookNumber=1;
    Code:
    stop();
    bookNumber=2;
    trace("you are at book2");
    Basically i can use the home_btn button anywhere but after my file runs through the "direction" frame, and onto "book1" and "book2". I suspect it is something to do with the if statements on the "direction" frame.

    I'm not sure how clear this is, and i'm asking for quite a bit, but if you could, it would really help! Thanks!!

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Once bookNumber is set to 1 or 2, nothing ever sets it back to anything else.

    I think the problem might be that you have whichBook as an enter_frame listener. It will be constantly sending your movie to either book1 or book2. Perhaps you could remove the listener on those frames.

  10. #10
    Junior Member
    Join Date
    Mar 2007
    Posts
    26
    Before i had the if statements by themselves and it didn't seem to work... is it possible?

    If i don't do onEnterFrame, what other options do i have?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center