1
votes

i write a few lines codes that when i click on the button, a random number will be generated. then a random child will be show up on the stage.

but i'm trying to when i click again on the button, the previous child has been removed and new child with new random number will be replaced.

how can i do that? or how can i find out what's the previous random number?

function clkBtn(evt:MouseEvent):void {

i=(Math.floor(Math.random()*10));

addChild(picP[i]);

removeChild(picP[?]);

}

1

1 Answers

0
votes

Just save a reference to the DisplayObject you are adding to the stage and then remove it on the next time the click function is called.

var displayObject:DisplayObject = null;

function clkBtn(evt:MouseEvent):void {

    if(displayObject) 
        removeChild(displayObject);     

    var i:int = Math.floor(Math.random()*10);

    displayObject = picP[i];
    addChild(displayObject);        
}