1

我只是不知道该怎么办。我已经浏览了一个小时,并且一直在阅读错误:

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

1 回答 1

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;
        }
    }
}
于 2013-01-22T05:32:18.540 回答