A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: unload uiloader content

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    102

    unload uiloader content

    I am trying to load content in the uiloader (a jpeg) and then unload it (remove the content but not the loader) when you get to a certain point.

    I tried the following code but it doesn't work. I even tried adding removeChild and it didn't remove the listener from the stage.

    Code:
    //start at screen one which loads the content
    var screenNum:int = 1
    trackScreenNum();
    
    // loades the content fine
    function baseOptions_Click(event:MouseEvent):void {
    	currentBtn = event.currentTarget;
    	oldBtnName = currentBtn.name.indexOf("Btn");
    	newCurrentBtn = currentBtn.name.substr(0, oldBtnName);
    	panel_mc.nextBtn.enabled=true;
    
    	for (var i:uint=0; i<xmlBgName.length(); i++) {
    		if (xmlBgName[i].@name == newCurrentBtn) {
    			prevBgImgUrl = url + xml.Backgrounds.color[i].@prevUrl;
    		}
    	}
    	if (currentMode == tutorials_mc.basicBtn.name) {
    		colorPreview.source = prevBgImgUrl;
    	} else if (currentMode == tutorials_mc.advBtn.name) {
    		colorImg.load(new URLRequest(prevBgImgUrl));
    		contHznBtns.hzColorBG.addChild(colorImg);
    
    	}
    }
    
    // if user gets to screen 0 then unload the content. I get the trace so i know the switch is happening.
     
    function trackScreenNum() {
    	switch (screenNum) {
    		case 0 :
    			trace("screen 0");
    			TransitionManager.start(tutorials_mc, {type:Fade, direction:Transition.IN, duration:1, easing:Strong.easeIn});
                            // doesn't unload the content
    			colorPreview.unload();
    			removeChild(colorPreview);
    			break;
    		case 1 :
    			panel_mc.myScroller.source=color_options;
    			callGlossyBase();
    			//panel_mc.backBtn.enabled=false;
    			panel_mc.nextBtn.enabled=false;
    			panel_mc.panelHeader.text = "CHOOSE YOUR BASE COLOR";
    			break;
    
    	}
    }
    Am i not calling the unload property properly?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I can't tell what colorPreview is, but it does not seem to be the loader that loaded the image (colorImg).

    If you were expecting it to unload the resource just because they use the same url, then no, that's not how things work.

  3. #3
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    colorPreview is a uiloader component that is on the stage with an instance name of colorPreview.

    I need a way to unload the content inside the loader or almost set it to nothing so that when i go to the next section via my switch statement it doesn't have that content.

    How do i go about just unloading the content? I thought i can use the unload property with a uiloader component?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I've never dealt with components. Are you sure that you're using the colorPreview in the first place? It looks like you have two different ways of loading the same things for some reason.

  5. #5
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    what i ultimately want is this.

    1. user clicks a button and it loads an image in the uiloader (colorpreview)
    2. user clicks a back button which takes them to a previous section (via switch stament).
    3. when user gets to screen 0 in the switch statement unload the content so that when a user goes back to the next section it doesn't show the old content but still keeps the loader on the stage so content can be loaded into.

    Note: i could easily just set the visibility the false for the loader when i got to a specific screen but the problem is that when i get to the next screen and set it to true (before the user clicks on a btn to load content into it) the user will see the old image that was previously loaded.

  6. #6
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    im fine with just using the loader class if you can come up with a way for it to work.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'd just throw the loader away instead of trying to unload it. You can remove it from the stage and set the reference to null (also remove any event listeners). Then check for null before using it on the frame that it should appear on.

  8. #8
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    so i tried doing colorPreview.removeChild() and it didn't seem to work. I also tried doing colorPreview.source = null and that didn't work either.

    Am i doing it wrong? Can you provide a snippet of code/example?

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Like I said, I'd use the loader, because I'm more familiar with it
    Code:
    var loader:Loader = null;
    //...
    //when you need to show it:
    if (loader == null){
      loader = new Loader();
      addChild(loader);
      loader.load(someURL);
    }
    
    //...
    //when you need to remove it:
    removeChild(loader);
    loader = null;

  10. #10
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    is there a way to find all the instance name of the loaders on the stage dynamically so that i don't have to do loader1.unload, loader2.unload, etc?

    I want to find all the loaders and unload content on all of them.

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could keep them in an array. Be careful when mixing that strategy with the "throw away" strategy, because the Array contents will NOT automatically update to the variable values. I mean something like this:
    Code:
    var allLoaders:Array = new Array();
    var loader:Loader;
    //...
    loader = new Loader();
    allLoaders.push(loader);
    //...
    loader = null; //loader var is now null, but allLoaders still contains the value pushed in.

  12. #12
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    essentially though there is no way to get all the instance names of the loaders on the stage dynamically without putting them in an array though?

    Is there a better way i should be doing this then you mentioned above in your last snippet?

  13. #13
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    is there a way to loop through all the variable names in my actionscript, find the variables names that have a data type of like loader and uiloader and then add those to the array? Would that be a way to do it?

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm a bit out of my element on this, actually. I rarely deal with multiple loaders, and certainly don't have much experience UNloading.

    An array or other collection is definitely the most efficient and cleanest way of handling collections of like objects that I know of. It obviates the need to get instance names at all, instead giving you actual instances.
    There are other ways you could iterate through your loaders of course. If you have a naming convention, you could use that. That's really not a great idea as it's far more fragile.

    You could write a displayTree traversal function which collects instances of Loader. This is less fragile, but also far less efficient. I used to use this technique a lot back when I had to deal with artist structured content and couldn't rely on anything other than the display tree. Here's a sketch of how it might work.

    Code:
    public var allLoaders:Array = collectAllLoaders();
    
    public function collectAllLoaders():Array{
      var toreturn:Array = new Array();
      traverseTree(root, loaderFilter, toreturn);
      return toreturn;
    }
    
    public function loaderFilter(obj:DisplayObject):Boolean{
      if (obj is Loader){
        return true;
      }else{
        return false;
      }
    }
    
    
    public function traverseTree(node:DisplayObjectContainer, filter:Function, collection:Array):void{
      var o:DisplayObject;
      for (var i:int = 0; i < node.numChildren; i++){
        o = node.getChildAt(i);
        if (filter(o)){
         collection.push(o);
        }
        if (o is DisplayObjectContainer){
          traverseTree(o as DisplayObjectContainer, filter, collection);
        }
      }
    }

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