简体   繁体   中英

Remove sprite from stage in as3

I have this code for a message to appear on stage when player finishes drag and drop. I would like this sprite to be removed when a button is clicked for the next frame. Can someone help me with the code?

 stage.addEventListener(Event.ENTER_FRAME, EntFrame);
 function EntFrame (e:Event):void 
  {
     if (CntP1+CntP2+CntP3+CntP4+CntP5+CntP6+CntP7+CntP8 == 40)
{
    var w:int = 400, h:int = 200;
    var win:Sprite = new Sprite();
    win.name = "Mywin";
    addChild(win);


 // draw rounded rect with subtle vertical linear gradient fill and blue stroke
    win.graphics.lineStyle(4,0x0077ff);
    var mat:Matrix = new Matrix();
    mat.createGradientBox(w, h, 90 * (Math.PI / 180));
    win.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0xeeeeee],[1.00,1.00],[0,255],mat);
    win.graphics.drawRoundRect(0,0,w,h,15,15);

    // show center "YOU WIN!" text
    var tf:TextField = new TextField();
    tf.autoSize = TextFieldAutoSize.LEFT;
    tf.antiAliasType = AntiAliasType.ADVANCED;
    tf.defaultTextFormat = new TextFormat("Arial, Verdana",36,0x454545,true);
    tf.text = "Κέρδισες!";
    tf.selectable = false;
    win.addChild(tf);
    tf.x = w/2 - tf.width/2;
    tf.y = h/2 - tf.height/2;

    // add a drop shadow
    var dropShadow:DropShadowFilter = new DropShadowFilter(3,45,0,.35,8,8,1,3);
    win.filters = [dropShadow];

    // center the graphic
    win.x = stage.stageWidth/2 - win.width/2;
    win.y = stage.stageHeight/2 - win.height/2;


}

}

Your code isn't written well and needs rewriting to ensure reuse or scalability of your project, but here's a quick way out.

make a holder Sprite, something like

var messageHolder:Sprite = new Sprite();
addChild(messageHolder);

add all the messages to that holder in any fashion you like. When you need to erase the contents of that holder, call following method:

function clearHolderContents(holder:DisplayObjectContainer):void
{
   if (holder.numChildren < 1)
   return; // no need to continue this method if the target is empty

   for (var i:int = holder.numChildren - 1; i >= 0; i--)
      removeChild(holder.getChildAt(i));
}

This method can clear contents of any DisplayObjectContainer => use it for your messageHolder:

clearHolderContents(messageHolder);

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM