A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Problems Removing Target Child

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25

    Problems Removing Target Child

    Hello, I've run into a problem when trying to move one of my sprites from the stage when it dies. So far what I'm having to do is call the Death method from the main class, which then goes to the Enemy Class in order to play the animation, once the animation has reached the last frame an event is triggered which is situated in the main frame which then removes the now dead enemy from the stage. However it doesn't appear to be able to target the child which I want removed =/ any ideas. Thanks.

    Main Class:
    Code:
    private function hitCheck(e:Event = null):void{
    			var samuraiPos:Number = SamuraiChar.samuraiPos();
    			if(SamuraiChar.currentAttackDirection() == Util.LEFT){
    				for(var i:uint = 0; i < enemyList.length; i++){
    					if(enemyList[i].x - samuraiPos > -150 && enemyList[i].x - samuraiPos < -50){
    						enemyList[i].die();
    						enemyList.splice(enemyList.indexOf(enemyList[i]), 1);	
    					}
    				}
    			} 
    		}
    private function removeEnemy(e:Event = null):void{
    			removeChild(this);
    			//enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
    		}
    Enemy Class:

    Code:
    public function die():void
    		{
    			EnemyChar.gotoAndStop(3);
    			if(EnemyChar.character.currentFrame == EnemyChar.character.totalFrames)
    				//this.dispatchEvent(new Event("removeEnemy"));
    				this.removeChild(EnemyChar);
    		}

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    removeChild is a method of DisplayObjectContainer. It must be called on the particular parent of the child to be removed. Your first attempt was more along the correct lines, but you should call removeChild(e.target) instead of removeChild(this).

    removeChild(this) never makes sense. An DisplayObject cannot hold itself as a child.

    There are some other oddities in the code posted. I do not know what EnemyChar is in Enemy, since I would expect that Enemy itself is the Enemy. Perhaps you've simply added an internal child to be the visual representation? That's not necessary, but also not too terrible. The event should probably bubble so that you don't have to add the listener to each Enemy instance. By default it does not.

  3. #3
    Senior Member
    Join Date
    Mar 2011
    Location
    Riverside ish...
    Posts
    173

    try something like this...

    Actionscript Code:
    if (this.y >= 600){
    removeSelf();
    }
    }

    function removeSelf() : void
            {
                if (MovieClip(parent).contains(this)){
                    MovieClip(parent).removeChild(this);
                    removeEventListener(Event.ENTER_FRAME,CrabGo);
            }
            }


    add what you need to and change everything around, but yea this works...

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If the parent property is not null, parent.contains(this) will always return true. If it is null, that will throw an error. So you can change removeSelf to this:
    Code:
    function removeSelf():void{
      if (parent){
        parent.removeChild(this);
      }
    I have no idea where CrabGo came from.

  5. #5
    Senior Member
    Join Date
    Mar 2011
    Location
    Riverside ish...
    Posts
    173
    its a segment from one of my files...

    its just to show what to call really quick, and to point out to remove any listeners that are associated to that clip!

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