开发者

AS3 Getting an Error #1009 on a Timer variable

开发者 https://www.devze.com 2023-02-13 06:26 出处:网络
I\'m having Error #1009: Cannot access a property or method of a null object reference.I\'m not exactly sure how to solve this problem myself, I\'ve traced to see if the object var enemySpawnTimer:Tim

I'm having Error #1009: Cannot access a property or method of a null object reference. I'm not exactly sure how to solve this problem myself, I've traced to see if the object var enemySpawnTimer:Timer is actually null and it isn't. So I don't understand why I'm getting this error.

Anyway here is my code, it's a class I use to spawn blocks that fall from the top of the screen to the bottom and is removed from the stage once it reaches the bottom of the screen.

package scripts
{
    import flash.events.Event;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Ti开发者_运维问答mer;
    import flash.display.DisplayObject;

    public class EnemySpawner
    {
        var stageRef:Stage;
        var target:Player;

        //vector variables
        public static var vectorBlock:Vector.<Block> = new Vector.<Block>();

        //enemy variables
        public static var block:Block;

        //timer variables
        var enemySpawnTimer:Timer = new Timer(250);

        //score variables
        public static var pointsBlock:Number = 0;

        public function EnemySpawner(stageRef:Stage, target:Player)
        {
            this.stageRef = stageRef;
            this.target = target;

            enemySpawnTimer.addEventListener(TimerEvent.TIMER, SpawnBlocks, false, 0, true);
            enemySpawnTimer.start();
        }

        private function SpawnBlocks(e:TimerEvent):void
        {
            block = new Block(stageRef);
            pointsBlock = block.pointsGiven;

            vectorBlock.push(block);
            stageRef.addChild(block);

            block.addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
        }

        private function Update(e:Event):void
        {
            //remove enemies when they pass the bottom of the stage
            for each(var block:Block in vectorBlock)
            {
                if(block.y > Bounds.rectH + block.height)
                {
                    RemoveFromList(block);
                }
            }

            target.addEventListener("playerDeath", StopSpawning, false, 0, true);
        }

        private function RemoveFromList(block:Block):void
        {
            vectorBlock.splice(vectorBlock.indexOf(block), 1);

            if(stageRef.contains(block))
            {
                stageRef.removeChild(block);
            }
        }

        private function StopSpawning(e:Event):void
        {
            enemySpawnTimer.stop();
        }
    }
}

This is the error I get, unfortunately using the script editor in flash is not too helpful with where or what is causing the error exactly.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at scripts::EnemySpawner/SpawnBlocks()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()


this error usually occur when something is null, my best guess is "stageRef" try adding

stageRef = new Stage();

before

this.stageRef = stageRef;

one more thing you can do for better debugging is to permit it, to do so click file/publish settings/flash and tick "permit debugging" and then republish it again to see your error, this time it will let you know the frame and line number that's causing the error.


If I am understanding the final point of error correctly, doesn't it have to be something within the SpawnBlocks() method that is null? I'd look at stageRef and vectorBlock, and perhaps block.pointsGiven.

0

精彩评论

暂无评论...
验证码 换一张
取 消