A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Removing Child isssue.. probably v easy problem..

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    76

    Removing Child isssue.. probably v easy problem..

    when this health crate goes below the screen and i try to remove it, it throws up error:

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:isplayObjectContainer/removeChild()
    at Game_new6_fla::MainTimeline/cratestuff()



    Code:
    var cratetimer:Timer=new Timer(3000);
    cratetimer.addEventListener (TimerEvent.TIMER, addcrate);
    cratetimer.start ();
    
    
    var mycrate:crate = new crate();//mybullet=instancename, bullet=class
    function addcrate (evt:TimerEvent):void{
        addChild (mycrate);
        mycrate.x=(mydude.width)+((stage.stageWidth-mydude.width)*Math.random());
        mycrate.y=((stage.stageHeight-(2*mydude.height))*Math.random());
    	
    }
    
    stage.addEventListener (Event.ENTER_FRAME, cratestuff);
    function cratestuff (evt:Event):void{
    	mycrate.y += sceneryspeed
    	if(mycrate.y>stage.stageHeight+10){ //removes crates below the screen
    					removeChild(mycrate);
    				}
    	if (mydude.hitTestObject(this.mycrate)){
    		
    		healthbar.scaleY=healthbar.scaleY*1.03;
    	}
    }
    What to do?

  2. #2
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    In your function addCrate, put this:

    Code:
    stage.addCrate(myCrate);
    instead of this:

    Code:
    addCrate(myCrate);
    and in your function cratestuff, put this:

    Code:
    stage.removeChild(myCrate);
    Try that, lemme know if that works.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    76
    nope still the same error

  4. #4
    Member
    Join Date
    Mar 2009
    Location
    Brooklyn, NY
    Posts
    77
    It may be because you're removing it in an onEnterFrame function. Which means that it is going to continue to try to remove and already removed crate.
    Try adding a boolean to make sure that the crate is only removed once.
    Code:
    var crateOnStage:Boolean = true;
    and then in the remove code
    Code:
    function cratestuff (evt:Event):void{
    if(crateOnStage)
    {
    	mycrate.y += sceneryspeed
    	if(mycrate.y>stage.stageHeight+10){ //removes crates below the screen
    					removeChild(mycrate);
                                            crateOnStage = false;
    				}
    	if (mydude.hitTestObject(this.mycrate)){
    		
    		healthbar.scaleY=healthbar.scaleY*1.03;
    	}
    }
    }
    Last edited by cessnajumpin; 05-01-2009 at 11:46 AM.
    Break it until it works.

  5. #5
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by jamrobot View Post
    nope still the same error
    Code:
    function cratestuff (evt:Event):void{
    	mycrate.y += sceneryspeed
    	if(mycrate.y>stage.stageHeight+10){ //removes crates below the screen
    					trace(mycrate);
    					trace(mycrate.parent);
    				}
    	if (mydude.hitTestObject(this.mycrate)){
    		
    		healthbar.scaleY=healthbar.scaleY*1.03;
    	}
    }

    lemme know what that returns in the output window.

  6. #6
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by cessnajumpin View Post
    It may be because you're removing it in an onEnterFrame function. Which means that it is going to continue to try to remove and already removed crate.
    Try adding a boolean to make sure that the crate is only removed once.
    Code:
    var crateOnStage:Boolean = true;
    and then in the remove code
    Code:
    if(mycrate.y>stage.stageHeight+10 && crateOnStage){
    crateOnStage = false;
    removeChild(mycrate);
    }
    Good call. Try that too.

  7. #7
    Member
    Join Date
    Mar 2009
    Posts
    76
    Quote Originally Posted by Beathoven View Post
    Code:
    function cratestuff (evt:Event):void{
    	mycrate.y += sceneryspeed
    	if(mycrate.y>stage.stageHeight+10){ //removes crates below the screen
    					trace(mycrate);
    					trace(mycrate.parent);
    				}
    	if (mydude.hitTestObject(this.mycrate)){
    		
    		healthbar.scaleY=healthbar.scaleY*1.03;
    	}
    }

    lemme know what that returns in the output window.
    [object crate]
    [object MainTimeline]
    [object crate]
    [object MainTimeline]
    [object crate]
    [object MainTimeline]

    is what comes up

  8. #8
    Member
    Join Date
    Mar 2009
    Posts
    76
    Quote Originally Posted by cessnajumpin View Post
    It may be because you're removing it in an onEnterFrame function. Which means that it is going to continue to try to remove and already removed crate.
    Try adding a boolean to make sure that the crate is only removed once.
    Code:
    var crateOnStage:Boolean = true;
    and then in the remove code
    Code:
    function cratestuff (evt:Event):void{
    if(crateOnStage)
    {
    	mycrate.y += sceneryspeed
    	if(mycrate.y>stage.stageHeight+10){ //removes crates below the screen
    					removeChild(mycrate);
                                            crateOnStage = false;
    				}
    	if (mydude.hitTestObject(this.mycrate)){
    		
    		healthbar.scaleY=healthbar.scaleY*1.03;
    	}
    }
    }
    this also doesnt work , same error every milisecond:


    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:isplayObjectContainer/removeChild()
    at Game_newstory_fla::MainTimeline/cratestuff()

  9. #9
    Senior Member
    Join Date
    May 2004
    Posts
    226
    A sloppy fix would be:
    if ( mycrate.parent ) mycrate.parent.removeChild(mycrate);

  10. #10
    Member
    Join Date
    Mar 2009
    Posts
    76
    Quote Originally Posted by v5000 View Post
    A sloppy fix would be:
    if ( mycrate.parent ) mycrate.parent.removeChild(mycrate);

    that is ok but when i restart the game, some crates stay on the stage and dont have any effect. Is there any way to get rid of all crate class objects on the next frame?

  11. #11
    Senior Member
    Join Date
    May 2004
    Posts
    226
    You could store all your crates in an array, then when you want to clear them call a function that loops through the array, removing them as needed. Something like below:
    PHP Code:
    var crates:Array = new Array();
    var 
    mycrate:crate = new crate();//mybullet=instancename, bullet=class
    function addcrate (evt:TimerEvent):void{
        
    crates.pushaddChild (mycrate) );
        
    mycrate.x=(mydude.width)+((stage.stageWidth-mydude.width)*Math.random());
        
    mycrate.y=((stage.stageHeight-(2*mydude.height))*Math.random());
        
    }
    function 
    clearCrates():void{
        while( 
    crates.length ){
            var 
    mycrate:crate crates.pop();
            if ( 
    mycrate.parent mycrate.parent.removeChild(mycrate);
        }


  12. #12
    Member
    Join Date
    Mar 2009
    Posts
    76
    what does pop mean?

  13. #13
    Member
    Join Date
    Mar 2009
    Location
    Brooklyn, NY
    Posts
    77
    pop removes an element from the end of an array. Also, for future posts, try to mention things like there is more than one crate with that instance name at the beginning of the post. It helps knowing that from the get go.
    Break it until it works.

  14. #14
    Member
    Join Date
    Mar 2009
    Posts
    76
    if i die and the crate is on the screen, it just stays there, whats a proper way to remove all children of a class or an array

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