【问题标题】:(ActionScript 3) Children and clearing the screen issue(ActionScript 3)儿童和清除屏幕问题
【发布时间】:2014-10-03 05:32:26
【问题描述】:

我最近一直在学习 ActionScript 3,在过去的 2 个小时里,我一直坚持这个:

正常使用时无法清屏

while (numChildren)
{
  removeChild(0)
}

我已经设法创建了这个应用程序,它允许我用 WAS 键控制一个绿点(我还没有完成 D 键>_>),但是它留下了痕迹,尽管我尽了最大努力清除屏幕。我来自 Java/C# 背景,所以如果我做错了什么,任何建议都将不胜感激。这是我的代码:

package 
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.KeyboardEvent;

/**
 * 
 * 
 */
public class Main extends Sprite 
{

    public var Player:EntityPlayer = new EntityPlayer();
    public var EntityDrawList:Array = new Array();
    public var EntityList:Array = new Array();
    public function Main():void 
    {
        addChild(Player.EntityGraphics);
        EntityDrawList.push(Player.EntityGraphics);
        EntityList.push(Player);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownEvent);
        UpdateScreen();
    }

    public function KeyDownEvent(Key:KeyboardEvent):void
    {
        if (Key.keyCode == 87)
        {
            Player.YLoc -= Player.EntitySpeed;
            UpdateScreen();
        } else if (Key.keyCode == 65) {
            Player.XLoc += Player.EntitySpeed;
            UpdateScreen();
        } else if (Key.keyCode == 83) {
            Player.YLoc += Player.EntitySpeed;
            UpdateScreen();
        }
    }

    public function UpdateScreen():void
    {

        while (numChildren > 0)
        {
            removeChildAt(0);
        }

        for (var e:int = 0; e < EntityList.length; e++)
        {
            addChild(EntityDrawList[e]);
            EntityList[e].drawEntity();
        }
    }

}

}

我创建了 2 个数组;一个将保存基类,另一个将保存绘图变量(是的,形状。)。可以看到这里显示的类(我只显示实体类,因为 Player 类只包含一个名为 Lives 的变量并扩展了实体类:

package  
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.ColorTransform;
/**
 * ...
 */
public class Entity extends Sprite
{

    public var XLoc:int = 0;
    public var YLoc:int = 0;

    public var EntityWidth:int = 20;
    public var EntityHeight:int = 20;
    public var EntitySize:int = 20;

    public var EntitySpeed:int = 1;

    public var EntityGraphics:Shape = new Shape();

    public var EntityTeam:int = 0;
    /**
     * Teams
     * 0: Player
     * 1: Ally
     * 2: Enemy
     * 3: Neutral
     * 4: Prop
     * 5: Tameable
     */

    public function Entity() 
    {

    }

    public function drawEntity():void
    {
        if (EntityTeam == 0)
        {
            EntityGraphics.graphics.beginFill(0x15FF00, 1);
            EntityGraphics.graphics.drawCircle(XLoc, YLoc, EntitySize);
            EntityGraphics.graphics.endFill();
        } else if (EntityTeam == 1) {
            //EntityGraphics.graphics.beginFill(0x, 1);
            //EntityGraphics.graphics.drawCircle(XLoc, YLoc, EntitySize);
        } else if (EntityTeam == 2) {
            EntityGraphics.graphics.beginFill(0xFF0000, 1);
            EntityGraphics.graphics.drawRect(XLoc, YLoc, EntityWidth, EntityHeight);
            EntityGraphics.graphics.endFill();
        }
    }

    public function eraseEntity():void
    {
        if (EntityTeam == 0)
        {
            EntityGraphics.graphics.beginFill(0xFFFFF, 1);
            EntityGraphics.graphics.drawCircle(XLoc, YLoc, EntitySize);
            EntityGraphics.graphics.endFill();
        } else if (EntityTeam == 1) {
            //EntityGraphics.graphics.beginFill(0x, 1);
            //EntityGraphics.graphics.drawCircle(XLoc, YLoc, EntitySize);
        } else if (EntityTeam == 2) {
            EntityGraphics.graphics.beginFill(0xFFFFFF, 1);
            EntityGraphics.graphics.drawRect(XLoc, YLoc, EntityWidth, EntityHeight);
            EntityGraphics.graphics.endFill();
        }

        drawEntity();
    }

    public function RemoveEntity():void
    {

    }

}

}

如果有更简单的清除屏幕的方法,Google 并不友好地分享它。提前感谢您的帮助:)

【问题讨论】:

  • 您应该使用容器剪辑并保留对所有内容的引用。你正在使用数组,所以你得到了那部分?所以移除一个玩家看起来像 mcPlayersHolder.removeChild(aPlayers[i]);使用数组引用剪辑。

标签: actionscript-3 actionscript adobe flash


【解决方案1】:

尝试使用容器的 removeChildren() 函数。

【讨论】:

  • 感谢@MasterRoro 的回答,但您能否更具体地说明一下我应该把这段代码放在哪里,或者如何使用它?
  • 当然。而不是你的 while(numChildren &gt; 0){} 循环。只需使用 removeChildren() 应该做同样的事情。
  • 答案提供了一个很好的选择,但正确的答案仍然是 removeChild(0) 应该产生错误,因为它是一个纯粹而简单的代码错误。 removeChilsAt(0) 是 PO 应该使用的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
相关资源
最近更新 更多