我得到了这个非常烦人的错误 “TypeError:错误#1009:无法访问空对象引用的属性或方法。 在monke2_fla :: MainTimeline / update2()“ 我找不到NULL var,除了#1009错误之外,任何帮助都会很好,因为脚本工作正常。
var randomX:Number = Math.random() * 800;
Banana_mc1.x = randomX;
Banana_mc1.y = 0;
var speed:Number = 10;
var speed2:Number = 5;
var speed3:Number = 2;
var speed4:Number = 6;
Banana_mc1.addEventListener(Event.ENTER_FRAME, moveDown);
var playerScore:int = 0;
var stopLoop = 0;
if (stopLoop == 1) { } else {
function moveDown(e:Event):void
{
Banana_mc1.y += speed;
Banana_mc2.y += speed2;
Banana_mc3.y += speed3;
Banana_mc4.y += speed3;
Banana_mc5.y += speed;
Banana_mc6.y += speed4;
Snake_mc1.y += speed2;
Bunch.y += speed3;
Snake_mc2.y += speed3;
}
}
// KEYS
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
//Monkey.addEventListener(Event.ENTER_FRAME, update);
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
Monkey.x -= 10;
if(event.keyCode == Keyboard.RIGHT)
Monkey.x += 10;
}
var hitAry:Array = [Banana_mc1,Banana_mc2,Banana_mc3,Banana_mc4,Banana_mc5,Banana_mc6];
var hitAry2:Array = [Snake_mc1,Snake_mc2];
stage.addEventListener(Event.ENTER_FRAME, update2);
function update2(e:Event):void
{
var hitAry:Array = [Banana_mc1,Banana_mc2,Banana_mc3,Banana_mc4,Banana_mc5,Banana_mc6];
var hitAry2:Array = [Snake_mc1,Snake_mc2];
for (var i:int=0; i < hitAry.length; i++) {
if (Monkey.hitTestObject(hitAry[i])) {
trace("HIT");
hitAry[i].parent.removeChild(hitAry[i]);
trace(hitAry[i]);
playerScore+=1;
playerScoreText.text = ("" + playerScore);
} else {
trace("MISS"); }
if (Floor.hitTestObject(hitAry[i])) {
trace("HIT The Floor");
hitAry[i].gotoAndPlay(35);
stopLoop = 1;
} else {
trace("MISS"); }
}
for (var b:int=0; b < hitAry2.length; b++) {
if (Monkey.hitTestObject(hitAry2[b])) {
Monkey.Head.gotoAndPlay(23);
trace("HIT");
hitAry2[b].parent.removeChild(hitAry2[b]);
playerScore-=1;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("MISS"); }
}
if (Monkey.hitTestObject(allleft)) {
Monkey.Head.gotoAndPlay(23);
trace("BITE");
playerScore-=100;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("NO BITE"); }
if (Monkey.hitTestObject(allright)) {
Monkey.Head.gotoAndPlay(23);
trace("BITE");
playerScore-=100;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("NO BITE"); }
if (Monkey.hitTestObject(Bunch)) {
trace("HIT BUNCH");
Bunch.parent.removeChild(Bunch);
playerScore+=10;
playerScoreText.text = ("" + playerScore);
}
}
这是一个过期的作业,我正在撞砖墙,提前致谢
答案 0 :(得分:0)
嗯,在您的代码底部附近,您会说:Bunch.parent.removeChild(Bunch);
但你也有:Banana_mc1.addEventListener(Event.ENTER_FRAME, moveDown);
调用一个不断引用束的函数。
你可以通过不删除束来测试。如果这解决了null var问题,那么你将不得不决定如何继续。你希望游戏在束消失后继续或结束吗?
然后还有这个:
hitAry[i].parent.removeChild(hitAry[i]);
trace(hitAry[i]);
您正在尝试在删除它后立即追踪它! 那可能会命中hitAry [i] null!