所以在我的游戏中,我使用一系列敌人来跟踪它们并更新它们等等 - 当玩家撞到敌人的游戏时,然而如果玩家射击击中敌人的子弹敌人被删除了。好吧那应该是会发生什么然而当子弹它的敌人它删除它但由于某种原因,该对象仍在更新,仍然可能崩溃到播放器?
我该怎样停止这个?
这是我在激光类中检查碰撞的代码
private function loop(e:Event) : void
{
//move bullet up
y -= bulletSpeed;
if (y < 0)
{
removeSelf();
}
for (var i:int = 0; i < AvoiderGame.enemyArray.length; i++)
{
if (hitTestObject(AvoiderGame.enemyArray[i]))
{
AvoiderGame.enemyArray[i].takeHit();
removeSelf();
}
}
}
这是我在敌人类中的所有代码。
package
{
import flash.display.MovieClip;
import flash.display.Stage;
public class Enemy extends MovieClip
{
private var stageRef:Stage;
public function Enemy(startX:Number, startY:Number, stageRef:Stage)
{
this.stageRef = stageRef;
x = startX;
y = startY;
}
public function moveDown():void
{
y = y + (1 + (10 - 1) * Math.random());
switch(Math.round(1 + (2 - 1) * Math.random()))
{
case 1: x = x + (1 + (10 - 1) * Math.random());
break;
case 2: x = x - (1 + (10 - 1) * Math.random());
break;
};
}
public function StayOnScreen():void
{
if (x <= 0)
{
x = 0;
}
else if (x >= stageRef.stageWidth)
{
x = stageRef.stageWidth;
}
else
{
x = x;
}
}
private function removeSelf() : void
{
if (stageRef.contains(this))
{
this.parent.removeChild(this);
delete AvoiderGame.enemyArray[this];
}
}
public function takeHit() : void
{
removeSelf();
}
}
}
这是我在主游戏中的所有代码。
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
import flashx.textLayout.formats.BackgroundColor;
public class AvoiderGame extends MovieClip
{
public static var enemyArray:Array;
public var enemy:Enemy
public var Background:gameBackground;
public var avatar:Avatar;
public var gameTimer:Timer;
private var _collisionTest:CollisionTest;
private var numStars:int = 80;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
public function AvoiderGame()
{
Background = new gameBackground();
addChild(Background);
enemyArray = new Array();
var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 2, stage);
enemyArray.push(enemy);
addChild(enemy);
avatar = new Avatar(stage);
addChild(avatar);
avatar.x = stage.stageWidth / 2;
avatar.y = stage.stageHeight / 2;
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
_collisionTest = new CollisionTest();
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
fireTimer = new Timer(300, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
fireTimer.start();
}
public function onTick(timerEvent:TimerEvent):void
{
if (Math.random() < 0.1)
{
var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
enemyArray.push(enemy);
addChild(enemy);
}
avatar.UpdateAvatar(canFire);
if (canFire == true)
{
canFire = false;
fireTimer.start();
}
avatar.StayOnScreen();
for each (var enemy:Enemy in enemyArray)
{
enemy.moveDown();
enemy.StayOnScreen();
if (_collisionTest.complex(enemy, avatar))
{
gameTimer.stop();
}
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
我明白使用removeSelf()函数可能会在敌人类中出错,但我该如何解决这个问题呢?
如果你没有得到我想要的意思 - 在我的网站上有一个可玩的例子,使用空间射击将摧毁敌人,但它仍将在游戏中?
http://dynamite-andy.co.uk/projects/free-time/as3-avoider-game/
答案 0 :(得分:0)
问题似乎出现在removeSelf()
方法中的这一行:
delete AvoiderGame.enemyArray[this];
要从Array
删除某些内容,您必须使用修改数组的Array class methods而不是delete
关键字。
<强> [编辑] 强> 在这种情况下,您可以使用splice()从阵列中删除敌人:
var enemyIndex:int = this.parent.getChildIndex(this);
this.parent.removeChild(this);
AvoiderGame.enemyArray.splice(enemyIndex, 1);