1

当玩家完成拖放时,我有此代码用于在舞台上显示一条消息。我希望在为下一帧单击按钮时删除此精灵。有人可以帮我写代码吗?

 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;


}

}

4

1 回答 1

0

您的代码写得不好,需要重写以确保项目的重用或可扩展性,但这里有一个快速的出路。

制作一个持有人Sprite,例如

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 的内容 => 将其用于您的 messageHolder:

clearHolderContents(messageHolder);

希望有帮助!

于 2013-08-21T01:19:25.023 回答