A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Remove loaders with removeChild

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Location
    Swansea, UK
    Posts
    12

    Remove loaders with removeChild

    Hello, I have a parent swf that loads in 6 little swfs. However, I have a button which should remove the loaders from the stage, no matter which one it is from the 6. At the moment I can only remove one of them because of the way(a stupid way) I have coded the application. So, when i click on the button which is supposed to remove whatever loader is currently sitting on top of my parent movie, it only removes one of the loaders - the one which is named first under my resetAssets function.

    I can not have:

    removeChild(ldr);
    removeChild(ldr2);
    removeChild(ldr3);
    etc.

    because only one of them is being displayed at the time and flash goes like "hey dude! where are the others you want me to remove then!?"

    I suppose the whole code has been built up poorly and I should loop the swfs through one loader instead of having a loader for each swf.. Then I could just remove the loader no matter what sits in there..?

    Well, I'm not good in coding and would appreciate help from anyone really as it is not a rare or difficult issue.


    Code:
    //imports
    import flash.filters.*;
    import flash.events.*;
    import flash.display.Loader;
    import flash.display.Sprite;	
    
    
    
    var ldr:Loader = new Loader();
    var req:URLRequest = new URLRequest("load_XML.swf");
    
    var ldr2:Loader = new Loader();
    var req2:URLRequest = new URLRequest("load_XML2.swf");
    
    var ldr3:Loader = new Loader();
    var req3:URLRequest = new URLRequest("load_XML3.swf");
    
    var ldr4:Loader = new Loader();
    var req4:URLRequest = new URLRequest("load_XML4.swf");
    
    var ldr5:Loader = new Loader();
    var req5:URLRequest = new URLRequest("load_XML5.swf");
    
    var ldr6:Loader = new Loader();
    var req6:URLRequest = new URLRequest("load_XML6.swf");
    
    
    building_key.addEventListener(MouseEvent.CLICK, onClickKey);
    building2_key.addEventListener(MouseEvent.CLICK, onClickKey);
    building3_key.addEventListener(MouseEvent.CLICK, onClickKey);
    library_key.addEventListener(MouseEvent.CLICK, onClickKey);
    track_key.addEventListener(MouseEvent.CLICK, onClickKey);
    roadblock_key.addEventListener(MouseEvent.CLICK, onClickKey);
    
    
    
    function onClickKey(evt:MouseEvent):void {
    	
    	switch(evt.target.name) {
    		case 'building_key':
    			ldr.load(req);
    			addChild(ldr);
    			break;
    		case 'building2_key':
    			ldr2.load(req2);
    			addChild(ldr2);
    			break;
    		case 'building3_key':
    			ldr3.load(req3);
    			addChild(ldr3);
    			break;
    		case 'library_key':
    			ldr4.load(req4);
    			addChild(ldr4);
    			break;
    		case 'track_key':
    			ldr5.load(req5);
    			addChild(ldr5);
    			break;
    		case 'roadblock_key':
    			ldr6.load(req6);
    			addChild(ldr6);
    			break;
    	}
    	
    }
    
    /*function removeAllChildren($do:DisplayObjectContainer):void {
    	while ($do.numChildren) {
    			$do.removeChildAt(0);
    		}
    	}
    */
    
    
    
    //Filters
    		 
    var myGlow:GlowFilter = new GlowFilter();
    var unGlow:GlowFilter = new GlowFilter();
    
    myGlow.color = 0x0070BC;
    myGlow.alpha = 0.65;
    myGlow.blurX = 15;
    myGlow.blurY = 15;
    myGlow.strength = 10;
    unGlow.inner=true;
    unGlow.color = 0x000000;
    unGlow.blurX = 0;
    unGlow.blurY = 0;
    
    
    stop();
    
    //Button Mode
    this.building_key.buttonMode = true;
    this.building2_key.buttonMode = true;
    this.building3_key.buttonMode = true;
    this.roadblock_key.buttonMode = true;
    this.track_key.buttonMode = true;
    this.library_key.buttonMode = true;
    this.closeMe.buttonMode = true;
    this.closeMe.visible = false;
    //........................................................ Functions
    function removeOverlisteners() {
    	removeBuildingListener();
    	removeBuildingListener2();
    	removeBuildingListener3();
    	removeBuildingListener4();
    	removeBuildingListener5();
    	removeBuildingListener6();
    	this.building_key.visible = false;
    	this.building2_key.visible = false;
    	this.building3_key.visible = false;
    	this.track_key.visible = false;
    	this.library_key.visible = false;
    	this.roadblock_key.visible = false;
    	buildingline1.visible = false;
    	buildingline2.visible = false;
    	buildingline3.visible = false;
    	trackline.visible = false;
    	roadblockline.visible = false;
    	libraryline.visible = false;
    	this.closeMe.visible = true;
    	
    }
    
    function resetAssets() {
    	this.building_key.gotoAndStop(1);
    	this.building2_key.gotoAndStop(1);
    	this.building3_key.gotoAndStop(1);
    	this.track_key.gotoAndStop(1);
    	this.library_key.gotoAndStop(1);
    	this.roadblock_key.gotoAndStop(1);
    	this.building_key.visible = true;
    	this.building2_key.visible = true;
    	this.building3_key.visible = true;
    	this.track_key.visible = true;
    	this.library_key.visible = true;
    	this.roadblock_key.visible = true;
    	this.buildingline1.visible = true;
    	this.buildingline2.visible = true;
    	this.buildingline3.visible = true;
    	this.trackline.visible = true;
    	this.roadblockline.visible = true;
    	this.libraryline.visible = true;
    	building_mc.filters = [unGlow];
    	building2_mc.filters = [unGlow];
    	building3_mc.filters = [unGlow];
    	track_mc.filters = [unGlow];
    	roadblock_mc.filters = [unGlow];
    	library_mc.filters = [unGlow];
    	this.building_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver ); 
    	this.building2_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver2 ); 
    	this.building3_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver3 ); 
    	this.library_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver4 ); 
    	this.track_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver5 ); 
    	this.roadblock_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver6 );
    	this.building_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut);
    	this.building2_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut2);
    	this.building3_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut3);
    	this.library_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut4);
    	this.track_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut5);
    	this.roadblock_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut6);
    	this.closeMe.visible = false;
    	removeChild(ldr);
    	removeChild(ldr2);
    	removeChild(ldr3);
    	removeChild(ldr4);
    	removeChild(ldr5);
    	removeChild(ldr6);
    	
    	
    }
    
    this.closeMe.addEventListener( MouseEvent.MOUSE_DOWN, resetMe);
    
    function resetMe(MouseEvent:Event):void {
    	resetAssets();
    	addDownListener();
    	addDownListener2();
    	addDownListener3();
    	addDownListener4();
    	addDownListener5();
    	addDownListener6();
    	
    }
    
    
    
    
    
    //........................................................ BUILDING 1
    //Building KEY Event Listeners
    this.building_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver ); 
    this.building_key.addEventListener( MouseEvent.MOUSE_DOWN, buildingDown);
    this.building_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut);
    
    
    function buildingOver (MouseEvent:Event):void {
    	building_mc.filters = [myGlow];
    	building_key.scaleX += .3; 
    	building_key.scaleY += .3; 
    	removeBuildingListener();
    }
    
    function buildingDown (MouseEvent:Event):void {
    	removeOverlisteners();
    	building_key.visible = true;
    	this.building_key.gotoAndStop(2);
    	building_key.scaleX -= .3; 
    	building_key.scaleY -= .3;
    	removeDownListener();
    	removeBuildingOut();
    //	ldr.load(req);
    //	addChild(ldr);
    	trace("Loader1");
    	ldr.x = 76;
    	ldr.y = 5;
    	
    }
    
    function buildingOut (MouseEvent:Event):void {
    	building_mc.filters = [unGlow];
    	building_key.scaleX -= .3; 
    	building_key.scaleY -= .3; 
    	addBuildingListener();
    	
    }
    
    function removeBuildingListener() {
    	this.building_key.removeEventListener(MouseEvent.MOUSE_OVER, buildingOver);
    }
    
    function addBuildingListener() {
    	this.building_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver);
    }
    /*
    function addBuildingOutListener() {
    	this.building_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut);
    }
    */
    
    function removeBuildingOut() {
     	this.building_key.removeEventListener( MouseEvent.MOUSE_OUT, buildingOut);
    }
    
    function removeDownListener() {
    	this.building_key.removeEventListener (MouseEvent.MOUSE_DOWN, buildingDown);
    }
    
    function addDownListener() {
    	this.building_key.addEventListener (MouseEvent.MOUSE_DOWN, buildingDown);
    }
    
    trace ("done");
    //........................................................ BUILDING 2
    //Building KEY Event Listeners
    this.building2_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver2 ); 
    this.building2_key.addEventListener( MouseEvent.MOUSE_DOWN, buildingDown2);
    this.building2_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut2);
    
    
    function buildingOver2 (MouseEvent:Event):void {
    	building2_mc.filters = [myGlow];
    	building2_key.scaleX += .3; 
    	building2_key.scaleY += .3; 
    	removeBuildingListener2();
    }
    
    function buildingDown2 (MouseEvent:Event):void {
    	removeOverlisteners();
    	building2_key.visible = true;
    	this.building2_key.gotoAndStop(2);
    	building2_key.scaleX -= .3; 
    	building2_key.scaleY -= .3;
    	removeDownListener2();
    	removeBuildingOut2();
    //	ldr2.load(req2);
    //	addChild(ldr2);
    	trace("Loader2");
    	ldr2.x = 76;
    	ldr2.y = 5;
    	
    }
    
    function buildingOut2 (MouseEvent:Event):void {
    	building2_mc.filters = [unGlow];
    	building2_key.scaleX -= .3; 
    	building2_key.scaleY -= .3; 
    	addBuildingListener2();
    	
    }
    
    function removeBuildingListener2() {
    	this.building2_key.removeEventListener(MouseEvent.MOUSE_OVER, buildingOver2);
    }
    
    function addBuildingListener2() {
    	this.building2_key.addEventListener(MouseEvent.MOUSE_OVER, buildingOver2);
    }
    
    function removeBuildingOut2() {
     	this.building2_key.removeEventListener( MouseEvent.MOUSE_OUT, buildingOut2);
    }
    /*
    function addBuildingOutListener2() {
    	this.building2_key.addEventListener( MouseEvent.MOUSE_OUT, buildingOut2);
    }
    */
    
    function removeDownListener2() {
    	this.building2_key.removeEventListener (MouseEvent.MOUSE_DOWN, buildingDown2);
    }
    
    function addDownListener2() {
    	this.building2_key.addEventListener (MouseEvent.MOUSE_DOWN, buildingDown2);
    }
    
    trace ("done");
    
    
    //Building 3
    
    //Building 4
    
    //Building 5
    
    //Building 6

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    //imports
    import flash.filters.*;
    import flash.events.*;
    import flash.display.Loader;
    import flash.display.Sprite;
    
    stop();
    
    var dict:Dictionary = new Dictionary();
    dict[this.building_key]={url:"load_XML.swf", mc:building_mc, aline:buildingline1, xval:76, yval:5};
    dict[this.building2_key]={url:"load_XML2.swf", mc:building2_mc, aline:buildingline2, xval:76, yval:5};
    dict[this.building3_key]={url:"load_XML3.swf", mc:building3_mc, aline:buildingline3, xval:76, yval:5};
    dict[this.roadblock_key]={url:"load_XML4.swf", mc:roadblock_mc, aline:roadblockline, xval:76, yval:5};
    dict[this.track_key]={url:"load_XML5.swf", mc:track_mc, aline:trackline, xval:76, yval:5};
    dict[this.library_key]={url:"load_XML6.swf", mc:library_mc, aline:libraryline, xval:76, yval:5};
    
    var ldr:Loader = new Loader();
    addChild(ldr);
    setListeners();
    
    closeMe.buttonMode=true;
    closeMe.visible=false;
    closeMe.addEventListener(MouseEvent.CLICK, resetAssets);
    
    function setListeners():void {
    	for (var i in dict) {
    		i.buttonMode=true;
    		i.addEventListener(MouseEvent.CLICK, doClick);
    		i.addEventListener(MouseEvent.MOUSE_OVER, doOver);
    		i.addEventListener(MouseEvent.MOUSE_OUT, doOut);
    	}
    }
    function clearListeners():void {
    	for (var i in dict) {
    		i.buttonMode=false;
    		i.removeEventListener(MouseEvent.CLICK, doClick);
    		i.removeEventListener(MouseEvent.MOUSE_OVER, doOver);
    		i.removeEventListener(MouseEvent.MOUSE_OUT, doOut);
    	}
    }
    function doClick(evt:MouseEvent):void {
    	hideAssets();
    	ldr.load(new URLRequest(dict[evt.currentTarget].url));
    	evt.currentTarget.visible=true;
    	evt.currentTarget.gotoAndStop(2);
    	evt.currentTarget.scaleX-=.3;
    	evt.currentTarget.scaleY-=.3;
    	ldr.x=dict[evt.currentTarget].xval;
    	ldr.y=dict[evt.currentTarget].yval;
    }
    function doOver(evt:Event):void {
    	dict[evt.currentTarget].mc.filters=[myGlow];
    	evt.currentTarget.scaleX+=.3;
    	evt.currentTarget.scaleY+=.3;
    }
    function doOut(evt:Event):void {
    	dict[evt.currentTarget].mc.filters=[unGlow];
    	evt.currentTarget.scaleX-=.3;
    	evt.currentTarget.scaleY-=.3;
    }
    function hideAssets():void {
    	clearListeners();
    	for (var i in dict) {
    		i.visible=false;
    		dict[i].aline.visible=false;
    	}
    	closeMe.visible=true;
    }
    function resetAssets(evt:Event=null):void {
    	setListeners();
    	for (var i in dict) {
    		i.gotoAndStop(1);
    		i.visible=true;
    		dict[i].mc.filters=[unGlow];
    		dict[i].aline.visible=true;
    	}
    	closeMe.visible=false;
    	ldr.unloadAndStop();
    }
    
    //Filters
    var myGlow:GlowFilter = new GlowFilter();
    var unGlow:GlowFilter = new GlowFilter();
    myGlow.color=0x0070BC;
    myGlow.alpha=0.65;
    myGlow.blurX=15;
    myGlow.blurY=15;
    myGlow.strength=10;
    unGlow.inner=true;
    unGlow.color=0x000000;
    unGlow.blurX=0;
    unGlow.blurY=0;

Tags for this Thread

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