dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Problems with removeChild();

  1. #1
    Member
    Join Date
    May 2006
    Posts
    48

    resolved [RESOLVED] Problems with removeChild();

    I have an mcButton called Unit that when clicked, addChild's another mcButton named attBtn. When Unit is clicked again, I need attBtn removed. Click once, add attBtn. Click again, remove attBtn, and so on. I can't seem to make it remove attBtn.

    Code:
    package{
    	
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.display.MovieClip;
    	import flash.display.DisplayObjectContainer;
    	
    	public class Unit extends MovieClip{
    		
    		private var actBtnsDisplayed:Boolean = false;
    		public function Unit(){
    			this.addEventListener(MouseEvent.ROLL_OVER, unitRollOver);
    			this.addEventListener(MouseEvent.ROLL_OUT, unitRollOut);
    			this.addEventListener(MouseEvent.CLICK, unitClick);
    		}
    		
    		private function unitRollOver(event:MouseEvent):void{
    			this.gotoAndStop(2);
    		}
    		private function unitRollOut(event:MouseEvent):void{
    			this.gotoAndStop(1);
    		}
    		private function unitClick(event:MouseEvent):void{
    			var actBtnsDisplayed:Boolean = false;
    			trace("X = " + this.x);
    			trace("Y = " + this.y);
    			
    			var attBtn:AttackButton = new AttackButton(this);
    			if(!actBtnsDisplayed){
    				actBtnsDisplayed = true;
    				addChild(attBtn);
    				trace(actBtnsDisplayed);
    			} else{
    				actBtnsDisplayed = false;
    				trace(actBtnsDisplayed);
    				attBtn.parent.removeChild(attBtn);
    				attBtn = null;
    				if(this is DisplayObjectContainer){
    					trace("This is a Display Object Container")
    				} else{
    					trace("Nope");
    				}
    				if(this.contains(attBtn)){
    					trace("Yes it Does");
    				}
    			}
    			attBtn.checkMe();
    			trace(this.numChildren);
    		}
    	}
    }
    Thank you in advance for your time.

    PS: When the attBtn is clicked, it also clicks the parent Unit button. Am I just going about this completely wrong?

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    It's because you're creating a new instance of attBtn within the function.

    var attBtn:AttackButton = new AttackButton(this); needs to be moved... here, try this...

    Code:
    package{
    	
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.display.MovieClip;
    	import flash.display.DisplayObjectContainer;
    	
    	public class Unit extends MovieClip{
    		
    		private var attBtn:AttackButton = new AttackButton(this);
    		private var actBtnsDisplayed:Boolean = false;
    		public function Unit(){
    			this.addEventListener(MouseEvent.ROLL_OVER, unitRollOver);
    			this.addEventListener(MouseEvent.ROLL_OUT, unitRollOut);
    			this.addEventListener(MouseEvent.CLICK, unitClick);
    		}
    		
    		private function unitRollOver(event:MouseEvent):void{
    			this.gotoAndStop(2);
    		}
    		private function unitRollOut(event:MouseEvent):void{
    			this.gotoAndStop(1);
    		}
    		private function unitClick(event:MouseEvent):void{
    			var actBtnsDisplayed:Boolean = false;
    			trace("X = " + this.x);
    			trace("Y = " + this.y);
    			
    			if(!actBtnsDisplayed){
    				actBtnsDisplayed = true;
    				addChild(attBtn);
    				trace(actBtnsDisplayed);
    			} else{
    				actBtnsDisplayed = false;
    				trace(actBtnsDisplayed);
    				attBtn.parent.removeChild(attBtn);
    				// don't null the button, just reuse it.
    				// attBtn = null;
    				if(this is DisplayObjectContainer){
    					trace("This is a Display Object Container")
    				} else{
    					trace("Nope");
    				}
    				if(this.contains(attBtn)){
    					trace("Yes it Does");
    				}
    			}
    			attBtn.checkMe();
    			trace(this.numChildren);
    		}
    	}
    }
    Last edited by Ralgoth; 09-09-2009 at 09:23 AM.
    Search first, asked questions later.

  3. #3
    Member
    Join Date
    May 2006
    Posts
    48
    Ralgoth, thank you very much, that did it.

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