【发布时间】:2013-01-21 21:26:19
【问题描述】:
我只是不知道该怎么办。我已经浏览了一个小时,并且一直在阅读错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Shooter_Enemy/shotHandler()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
当我调试它时,它指向将“seekingBullet”添加到舞台的代码行。非常欢迎任何解决此问题的帮助。
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
public class Shooter_Enemy extends MovieClip
{
private var yMove:int = 2;
private var shootTimer:Timer = new Timer(500);
public function Shooter_Enemy()
{
this.name = "mc_shooter_enemy";
this.addEventListener(Event.ENTER_FRAME,enemyMove);
shootTimer.start();
shootTimer.addEventListener(TimerEvent.TIMER,shotHandler);
}
public function addShooterEnemy(X:int):void
{
this.x = X;
this.y = 0;
}
public function removeEnemy()
{
shootTimer.removeEventListener(TimerEvent.TIMER,shotHandler);
shootTimer.stop();
this.removeEventListener(Event.ENTER_FRAME,enemyMove);
this.x = 0;
this.y = (stage.height + this.height);
}
private function shotHandler(te:TimerEvent):void
{
var seekingBullet:SeekingBullet = new SeekingBullet();
Main.seekingBulletArray.push(seekingBullet);
stage.addChild(seekingBullet);
seekingBullet.addSeekingBullet(this.x,this.y);
}
private function enemyMove(e:Event)
{
this.y += yMove;
}
}
}
【问题讨论】:
-
Main 怎么样?它在哪里初始化并在 seekBulletArray 创建?
-
trace(stage);在stage.addChild之前会发生什么 -
在 Shooter_Enemy 实际添加到舞台后尝试启动计时器;即在Shooter_Enemy构造函数中,为Event.ADDED_TO_STAGE添加一个事件监听器
-
我尝试在添加孩子之前跟踪舞台,它始终输出 [object Stage] 直到射手敌人被摧毁,此时它输出 null 并给我 TypeError。所以它肯定是stage.addChild中的“舞台”。至于如何处理这些信息,我不知道......有什么想法吗?
-
编辑:实际上我只是通过在代码周围添加:
if (stage)来修复它,因此它仅在阶段为真时运行。修复了错误,但如果有人要添加/使其更有效,那么无论如何建议离开。感谢您的帮助!