我最近一直在学习ActionScript 3,在过去的两个小时里我一直坚持这个:
使用通常的
时我无法清除屏幕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的变量并扩展了Entity类:
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并不友好地分享它。在此先感谢您的任何帮助:)
答案 0 :(得分:0)
尝试使用容器的removeChildren()函数。