嗨,我有一个问题,我想我可能知道是什么导致它,但我不知道如何解决它。我有人可以帮我解决这个问题它会很棒...错误是
TypeError:错误#1009:无法访问空对象引用的属性或方法。 在Bullet / removeSelf()[C:\ Users \ Alan \ Desktop \ game copy \ Bullet.as:56] 在Bullet / loop()[C:\ Users \ Alan \ Desktop \ game copy \ Bullet.as:44]
这是主要行动的代码,一个可以移除BULLET ps的代码。对不起盖帽。
stage.addEventListener(Event.ENTER_FRAME, testCollisions);
//Check for collisions between an enemies array and a Lasers array
function testCollisions(e:Event):void
{
var tempEnemy:MovieClip;
var tempLaser:MovieClip;
for (var i:int=enemies.length-1; i >= 0; i--)
{
tempEnemy = enemies[i];
for (var j:int=bullets.length-1; j>=0; j--)
{
tempLaser = bullets[j];
if (tempLaser.hitTestObject(tempEnemy))
{
removeChild(tempEnemy);
removeLaser(j);
}
}
}
}
function removeLaser(idx:int)
{
parent.removeChild(bullets[idx]);
bullets.splice(idx,1);
}
这是删除它的BULLET类的代码
public class Bullet extends MovieClip {
private var speed:int = 30;
private var initialX:int;
public var eligableForRemove:Boolean = false;
public function Bullet(playerX:int, playerY:int, playerDirection:String) {
// constructor code
if(playerDirection == "left") {
speed = -30; //speed is faster if player is running
x = playerX - 25;
} else if(playerDirection == "right") {
speed = 30;
x = playerX + 25
}
y = playerY - 75;
initialX = x; //use this to remember the initial spawn point
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(e:Event):void
{
//looping code goes here
x += speed;
if(speed > 0) { //if player is facing right
if(x > initialX + 640) { //and the bullet is more than 640px to the right of where it was spawned
removeSelf(); //remove it
eligableForRemove = true;
}
} else if (speed < 0) { //else if player is facing left
if(x < initialX - 640) { //and bullet is more than 640px to the left of where it was spawned
removeSelf(); //remove it
eligableForRemove = true;
} else {
eligableForRemove = false;
}
}
}
public function removeSelf():void
{
if(eligableForRemove == true){trace("remove self");
removeEventListener(Event.ENTER_FRAME, loop); //stop the loop
this.parent.removeChild(this); //tell this object's "parent object" to remove this object
//in our case, the parent is the background because in the main code we said:
//back.addChild(bullet);
}
}
}
}
我认为导致它的原因是,当没有任何东西需要删除时,它正在调用一个空函数removeSelf。所以我添加了eligableForRemove变量,但我可能没有正确放置它所以如果有人可以帮我解决这个问题我会很感激...如果我尝试从主要动作中删除子弹它给我它必须是呼叫者错误的孩子。请帮助。
答案 0 :(得分:0)
你是对的,这可能是因为尝试删除一个孩子两次,而你在之后设置了eligableForRemove
,所以你致电removeSelf()
,所以{{1没有看到更新的值。
但是,我认为问题是你要删除removeSelf()
中的子弹,但不告诉子弹它已经死了,所以它继续运行循环,最终它超出界限并试图再次移除它。
您应该能够摆脱removeLaser()
,只需将eligableForRemove
更改为:
removeLaser()
(或者如果function removeLaser(idx:int)
{
(bullets[idx] as Bullet).removeSelf();
bullets.splice(idx,1);
}
是bullets
,您可以Vector.<Bullet>
,因为运行时已经知道它是bullets[idx].removeSelf()
。)