TyroneBiggums
TyroneBiggums

Reputation: 51

Why do I get parameter hitTestObject must be non-null error?

I am creating my first flash game with Actionscript3 and Adobe Flash CS5.5. The concept of the game is just going to be you're in a spaceship, and you try to avoid asteroids. The longer you avoid the asteroids the more points you get. I know it is not very original. Suddenly today I got this error:

TypeError: Error #2007: Parameter hitTestObject must be non-null.

What this error is doing is that when the space ship hits the asteroids nothing happens. The spaceship just flies right through.

I have no idea how to fix this. Here is my code:

public class AvoiderGame extends MovieClip
{   
    public var army:Array;
    public var gameTimer:Timer;
    public var avatar:Avatar;

    public function AvoiderGame()
    {
        army=new Array();

        avatar=new Avatar();
        addChild( avatar );
        avatar.x=mouseX;
        avatar.y=mouseY;

        gameTimer= new Timer (25);
        gameTimer.addEventListener(TimerEvent.TIMER, onTick);
        gameTimer.start();

    }
    public function onTick(timerEvent:TimerEvent):void
    {
        if (Math.random() < 0.1)
        {
            var randomX:Number = Math.random() * 400;
            var newEnemy=new Enemy(randomX,-15);
            army.push( newEnemy );
            addChild( newEnemy );
        }

            avatar.x=mouseX;
            avatar.y=mouseY;
            for each ( var enemy:Enemy in army ) 

            enemy.moveDownABit();
        {
            if (avatar.hitTestObject( enemy ))
            {
                gameTimer.stop();
                dispatchEvent( new AvatarEvent( AvatarEvent.DEAD ) );
            }
        }
    }
}

}

Upvotes: 0

Views: 2808

Answers (1)

M. Laing
M. Laing

Reputation: 1617

Looks like your brackets are messed up. The for each is only doing the enemy.moveDownABit() line. Move your bracket above that to right after the for loop starts.

With the bracket where it is right now, enemy is going out of scope, which is why it is Null which is what the error is telling you.

Upvotes: 1

Related Questions