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

4 posts

Flag Post

I’m making the Shootorial game in AS3. I’m following it step by step and I’m on page 4 of this part: http://www.kongregate.com/games/Moly/shootorial-5-actionscript-3?acomplete=shootorial+%234%3A+actionscript+3

I have a code that is supposed to make it so that when the player (ship) collides with an enemy, the enemy (EnemyShip) is removed from the stage.

Here is the code, which I have copied from the Shootorial:

package{
	import flash.display.MovieClip;
	import flash.events.Event;
	public class EnemyShip extends MovieClip{
	
		var speed:Number;
		
		function EnemyShip(){
			this.x = 700;
			this.y = Math.random()*200 + 50;
			speed = Math.random()*5 + 5;
			
			addEventListener("enterFrame", enterFrame);
			
		}//End EnemyShip
			function enterFrame(e:Event){
				this.x -= speed;
				if(this.x < -100){
				removeEventListener("enterFrame", enterFrame);
				stage.removeChild(this);
				}//End if statement
				
			if(this.hitTestObject(Game.ship)){
				kill();
			}//End if statement
				
			
			}//End enterFrame function
				function kill(){
					removeEventListener("enterFrame", enterFrame);
					stage.removeChild(this);
				}//End kill function
		
	}//End movieclip
}//End package

Whenever I try to test the movie, when I hit an enemy, it doesn’t disappear. I just constantly get this error in the output.

TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at EnemyShip/::enterFrame()

I don’t know what it means, so it would be nice if you would help me.

Thanks in advance,
Asger.

 
Flag Post

Is ship a static variable inside Game?

If it isn’t, the error is in the line if(this.hitTestObject(Game.ship)){ , because you can’t reference the Game class, you must reference an instance of it.

If it is static, then the property ship in the Game class is null.

 
Flag Post

You indenting is complete garbage, and the ‘//end this or that’ comments are not necessary if you align braces properly. You really need to learn to organize your code better.

Oh, and and you are getting this error because Game.ship was null when if(this.hitTestObject(Game.ship)) got executed.

 
Flag Post

Looks like the the if(this.hitTestObject(Game.ship)) statement can’t see Game.ship

Make sure you’ve made the ship object static, as page 2 of that same tutorial. Also check if your main class is called ‘Game.as’ and your ship Movieclip is called ‘ship’. Uppercase/Lowercase matters.