下面是一个在街道上生成公民的类。他们左右行走,并被分配他们将面对的一侧,他们移动的速度和他们的 y 位置。我之前在构造函数中使用了 setUpCitizens() 函数,但我想使用 ADDED_TO_STAGE 和 REMOVED_FROM_STAGE,因为我无法删除关卡和侦听器。
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.utils.Timer;
public class Citizen extends MovieClip
{
private var dx:Number;// speed and direction
private var lastTime:int;// animation time
public function Citizen(side:String, _speed:Number, yPos:Number)
{
var side = side;
var speed = _speed;
var yPos = yPos;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
setUpCitizens();
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeAllCitizens();
this.removeEventListener(Event.ENTER_FRAME,moveCitizen);
}
private function setUpCitizens():void
{
if (side == "left")
{
this.x = -40;// start to the left
dx = _speed;// walk left to right
this.scaleX = 1;// reverse
}
else if (side == "right")
{
this.x = 5720;// start to the right
dx = - _speed;// walk right to left
this.scaleX = -1;
}// not reverse
this.y = yPos;// vertical position
this.gotoAndStop(Math.floor(Math.random()*9+1));
// set up animation;
this.addEventListener(Event.ENTER_FRAME,moveCitizen);
lastTime = getTimer();
}
private function removeAllCitizens():void
{
removeEventListener(Event.ENTER_FRAME,moveCitizen);
parent.removeChild(this);
}
我收到此错误:
我这样称呼班级:
var c:Citizen = new Citizen(side,speed,yPos);
这是从父类对象 Level1 调用的。我也无法尝试引用所有生成的孩子并将它们全部删除以希望加快游戏速度......
感谢您的任何建议?