从as3中的阶段删除精灵

时间:2013-08-20 21:37:57

标签: actionscript-3

我有这个代码,当播放器完成拖放时,会在舞台上显示一条消息。我希望在单击下一帧的按钮时删除此精灵。有人可以帮我代码吗?

 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;


}

}

1 个答案:

答案 0 :(得分:0)

您的代码编写得不好,需要重写以确保项目的重用或可扩展性,但这是一个快速的解决方法。

制作持有人精灵,类似

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

以您喜欢的任何方式将所有消息添加到该持有者。当您需要删除该持有者的内容时,请调用以下方法:

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));
}

此方法可以清除任何DisplayObjectContainer =&gt;的内容。将它用于你的messageHolder:

clearHolderContents(messageHolder);

希望有所帮助!