Skip to main content
Known Participant
January 24, 2018
Question

Error 2007: Parameter hitTestObject must be non-null.

  • January 24, 2018
  • 1 reply
  • 1549 views

Hello,

I'm continue to get these errors but mainly Error 2007:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at 15SpaceShooter_fla::MainTimeline/moveShip()[15SpaceShooter_fla.MainTimeline::frame3:48]

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

at flash.display::DisplayObject/_hitTest()

at flash.display::DisplayObject/hitTestObject()

at Enemy/eFrame()

Here is my Enemy.as file

package{

//we have to import certain display objects and events

import flash.display.MovieClip;

import flash.events.*;

//this just means that Enemy will act like a MovieClip

public class Enemy extends MovieClip{

//VARIABLES

//this will act as the root of the document

//so we can easily reference it within the class

private var _root:Object;

//how quickly the enemy will move

private var speed:int = 5;

//this function will run every time the Bullet is added

//to the stage

public function Enemy(){

//adding events to this class

//functions that will run only when the MC is added

addEventListener(Event.ADDED, beginClass);

//functions that will run on enter frame

addEventListener(Event.ENTER_FRAME, eFrame);

}

private function beginClass(event:Event):void{

_root = MovieClip(root);

}

private function eFrame(event:Event):void{

//moving the bullet up screen

y += speed;

//making the bullet be removed if it goes off stage

if(this.y > stage.stageHeight){

removeEventListener(Event.ENTER_FRAME, eFrame);

_root.removeChild(this);

}

//checking if it is touching any bullets

//we will have to run a for loop because there will be multiple bullets

for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){

//numChildren is just the amount of movieclips within

//the bulletContainer.

//we define a variable that will be the bullet that we are currently

//hit testing.

var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);

//now we hit test

if(hitTestObject(bulletTarget)){

//remove this from the stage if it touches a bullet

removeEventListener(Event.ENTER_FRAME, eFrame);

_root.removeChild(this);

//also remove the bullet and its listeners

_root.bulletContainer.removeChild(bulletTarget);

bulletTarget.removeListeners();

//up the score

_root.score += 5;

}

}

//hit testing with the user

if(hitTestObject(playerShip)){

//losing the game

_root.gameOver = true;

removeEventListener(Event.ENTER_FRAME, eFrame);

this.parent.removeChild(this);

_root.gotoAndStop('lose');

}

}

public function removeListeners():void{

this.removeEventListener(Event.ENTER_FRAME, eFrame);

}

}

}

And here is my file for actions

stop();

//these booleans will check which keys are down

var leftDown:Boolean = false;

var upDown:Boolean = false;

var rightDown:Boolean = false;

var downDown:Boolean = false;

//how fast the character will be able to go

var mainSpeed:int = 5;

//how much time before allowed to shoot again

var cTime:int = 0;

//the time it has to reach in order to be allowed to shoot (in frames)

var cLimit:int = 12;

//whether or not the user is allowed to shoot

var shootAllow:Boolean = true;

//how much time before another enemy is made

var enemyTime:int = 0;

//how much time needed to make an enemy

//it should be more than the shooting rate

//or else killing all of the enemies would

//be impossible 😮

var enemyLimit:int = 16;

//the player's score

var score:int = 0;

//this movieclip will hold all of the bullets

var bulletContainer:MovieClip = new MovieClip();

addChild(bulletContainer);

//whether or not the game is over

var gameOver:Boolean = false;

//adding a listener to mcMain that will move the character

playerShip.addEventListener(Event.ENTER_FRAME, moveShip);

function moveShip(event:Event):void{

//checking if the key booleans are true then moving

//the character based on the keys

if(leftDown){

playerShip.x -= mainSpeed;

}

if(upDown){

playerShip.y -= mainSpeed;

}

if(rightDown){

playerShip.x += mainSpeed;

}

if(downDown){

playerShip.y += mainSpeed;

}

//keeping the main character within bounds

if(playerShip.x <= 0){

playerShip.x += mainSpeed;

}

if(playerShip.y <= 0){

playerShip.y += mainSpeed;

}

if(playerShip.x >= stage.stageWidth - playerShip.width){

playerShip.x -= mainSpeed;

}

if(playerShip.y >= stage.stageHeight - playerShip.height){

playerShip.y -= mainSpeed;

}

//Incrementing the cTime

//checking if cTime has reached the limit yet

if(cTime < cLimit){

cTime ++;

} else {

//if it has, then allow the user to shoot

shootAllow = true;

//and reset cTime

cTime = 0;

}

//adding enemies to stage

if(enemyTime < enemyLimit){

//if time hasn't reached the limit, then just increment

enemyTime ++;

} else {

//defining a variable which will hold the new enemy

var newEnemy = new Enemy();

//making the enemy offstage when it is created

newEnemy.y = -1 * newEnemy.height;

//making the enemy's x coordinates random

//the "int" function will act the same as Math.floor but a bit faster

newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));

//then add the enemy to stage

addChild(newEnemy);

//and reset the enemyTime

enemyTime = 0;

}

//updating the score text

scoreField.text = 'Score: '+score;

}

//this listener will listen for down keystrokes

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);

function checkKeysDown(event:KeyboardEvent):void{

//making the booleans true based on the keycode

//WASD Keys or arrow keys

if(event.keyCode == 37 || event.keyCode == 65){

leftDown = true;

}

if(event.keyCode == 38 || event.keyCode == 87){

upDown = true;

}

if(event.keyCode == 39 || event.keyCode == 68){

rightDown = true;

}

if(event.keyCode == 40 || event.keyCode == 83){

downDown = true;

}

//checking if the space bar is pressed and shooting is allowed

if(event.keyCode == 32 && shootAllow){

//making it so the user can't shoot for a bit

shootAllow = false;

//declaring a variable to be a new Bullet

var newBullet:Bullet = new Bullet();

//changing the bullet's coordinates

newBullet.x = playerShip.x + playerShip.width/2 - newBullet.width/2;

newBullet.y = playerShip.y;

//then we add the bullet to stage

bulletContainer.addChild(newBullet);

}

}

//this listener will listen for keys being released

stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);

function checkKeysUp(event:KeyboardEvent):void{

//making the booleans false based on the keycode

if(event.keyCode == 37 || event.keyCode == 65){

leftDown = false;

}

if(event.keyCode == 38 || event.keyCode == 87){

upDown = false;

}

if(event.keyCode == 39 || event.keyCode == 68){

rightDown = false;

}

if(event.keyCode == 40 || event.keyCode == 83){

downDown = false;

}

}

I'm not sure but it keeps specifiying Line 57 on enemy.as and then moveShip which cause Frame 3 of my Space Shooter game to act up

Thanks

Luke

This topic has been closed for replies.

1 reply

Colin Holgate
Inspiring
January 24, 2018

You're doing the hit test with a root level movieclip named playerShip, but you're saying this:

if(hitTestObject(playerShip)){ 

instead of:

if(hitTestObject(_root.playerShip)){ 

ChevyLukeAuthor
Known Participant
January 24, 2018

I just realized that, but it's still stating those two errors...

Thanks

Luke

Colin Holgate
Inspiring
January 24, 2018

Try adding 'this' to the hittestobject lines:

if(this.hitTestObject(bulletTarget)){

if(this.hitTestObject(playerShip)){