0
votes

I set a state every time I press a button. Then it goes to a frame and in the action script, whatever the state, it add a child to the stage and change the state again, but when you click the button again the child removes and state is changed back to previous state and the same child gets added again. Here is my code:

// first frame and will never come back to this one
function blueTurnValve(event:MouseEvent) // to go to the frame
{
gotoAndPlay("blue");
STATE = 1;
}


var redscreen:redfilling = new redfilling();
if (STATE == 2)
{
removeChild(redscreen);
STATE = 1;
}

if (STATE == 1)
{   
addChildAt(redscreen,2);
STATE = 2;
}

//after the child was added
function blueTurnValve1(event:MouseEvent)
{
gotoAndPlay("blue");
}

but it won't work, I get the error in the output(just before removing the child):

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at p1_fla::MainTimeline/frame2()[p1_fla.MainTimeline::frame2:35] at flash.display::MovieClip/gotoAndPlay() at p1_fla::MainTimeline/blueTurnValve1()[p1_fla.MainTimeline::frame30:19]

what is wrong with my code?

1
if (STATE == 2) { removeChild(redscreen); STATE = 1; } - JBin
Hiq much time elapses between adding 'redscreen' and removing it? And what is 'redscreen' the cild of? - Craig

1 Answers

0
votes

You need to use removeChildAt() since at STATE==1.

You are adding it as addChildAt(redscreen,2);

Try, removing it like so,

removeChildAt(2);