-
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?
-
In your function addCrate, put this:
Code:
stage.addCrate(myCrate);
instead of this:
and in your function cratestuff, put this:
Code:
stage.removeChild(myCrate);
Try that, lemme know if that works.
-
nope still the same error
-
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.
-
Originally Posted by jamrobot
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.
-
Originally Posted by cessnajumpin
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.
-
Originally Posted by Beathoven
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
-
Originally Posted by cessnajumpin
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()
-
A sloppy fix would be:
if ( mycrate.parent ) mycrate.parent.removeChild(mycrate);
-
Originally Posted by v5000
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?
-
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.push( addChild (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);
}
}
-
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|