AS3 - 带定时器的TypeError#1009

时间:2013-01-21 21:26:20

标签: actionscript-3 typeerror

我只是不知道该怎么做。我现在已经看了一个小时了,我一直在读错误:

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;
        }
    }
}    

1 个答案:

答案 0 :(得分:0)

如果是实际的,使用舞台的一个好习惯是听* Event.ADDED_TO_STAGE *然后开始你的活动,如:

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.addEventListener(TimerEvent.TIMER, shotHandler);
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }

        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 addedToStageHandler(e:Event)
        {
            shootTimer.start();
        }
        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;
        }
    }
}