0
votes

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

I am working on a game right now and trying to get it so the player will collide with the ground (he already collides with table)

It says the problem is happening in playerOneCollisions

table and ground are just classes with the basic class code linked to movieclips, they are extended movieclips (if that matters), and they have imported stage and movieclip (again, if that matters)

(the parts I think are important)

Player.as

public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;

        public function playerOneCollisions(table, ground)
        {
            if(this.hitTestObject(table))
            {
                trace("tabled");
                animation = "kong";
            }
            if(this.hitTestObject(ground))
            {
                trace("grounded");
                animation = "idle";
            }
        }

The function playerOneCollisions is called in Main.as

public function GameClock(timerEvent:TimerEvent):void
        {
            player.playerOnePress();
            player.playerOneKeyResults();
            player.playerOneCollisions(table, ground);
            player.playerAnimaitons();
        }

The rest of the code incase I was wrong about the important parts

Player.as

package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import KeyObject;
    import Table;

    public class Player extends MovieClip
    {
        public var stageRef:Stage;
        public var key:KeyObject;
        public var main:Main;
        public var table:Table;
        public var player:Player;
        public var ground:Ground;       

        public var leftPressed:Boolean = false;
        public var rightPressed:Boolean = false;
        public var upPressed:Boolean = false;
        public var downPressed:Boolean = false;

        public var grounded:Boolean = false;

        public var animation:String = "idle";

        public var runSpeed:Number = 5;

        public var animationState:String = "idle";

        public function Player (stageRef:Stage, X:int, Y:int):void
        {
            this.stageRef = stageRef;
            this.x = X;
            this.y = Y;

            key = new KeyObject(stageRef);
        }
        public function playerOnePress()
        {
            if(key.isDown(37) || key.isDown(65)){ // if left arrow or A is pressed
                leftPressed = true;
                //trace("left pressed");
            } else {
                leftPressed = false;
            }

            if(key.isDown(38) || key.isDown(87)){ // if up arrow or W is pressed
                upPressed = true;
                //trace("up pressed");
            } else {
                upPressed = false;
            }

            if(key.isDown(39) || key.isDown(68)){ //if right arrow or D is pressed
                rightPressed = true;
                //trace("right pressed");
            } else {
                rightPressed = false;
            }

            if(key.isDown(40) || key.isDown(83)){ //if down arrow or S is pressed
                downPressed = true;
                //trace("down pressed");
            } else {
                downPressed = false;
            }
        }
        public function playerOneKeyResults()
        {
            if(leftPressed)
            {
//              trace("left");
                this.x -= runSpeed;
            }else if(rightPressed)
            {
//              trace("right");
                this.x += runSpeed;
            }
        }
        public function playerOneCollisions(table, ground)
        {
            if(this.hitTestObject(table))
            {
                trace("tabled");
                animation = "kong";
            }
            if(this.hitTestObject(ground))
            {
                trace("grounded");
                animation = "idle";
            }
        }
        public function playerAnimaitons()
        {
            if(animation == "kong")
            {
                this.gotoAndStop(2);
            }
            if(animation == "idle")
            {
                this.gotoAndStop(1);
            }
        }
    }
}

Main.as

package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class Main extends MovieClip
    {
        public var gameTimer:Timer;
        public var player:Player;
        public var table:Table;
        public var ground:Ground;

        public function Main():void
        {
            gameTimer = new Timer (30);
            gameTimer.addEventListener(TimerEvent.TIMER, GameClock);
            gameTimer.start();

            player = new Player(stage, 80, 420);
            stage.addChild(player);
        }
        public function GameClock(timerEvent:TimerEvent):void
        {
            player.playerOnePress();
            player.playerOneKeyResults();
            player.playerOneCollisions(table, ground);
            player.playerAnimaitons();
        }   
    }
}
1

1 Answers

0
votes

One thing I notice is that you have table and ground as class properties of Player, and they are also parameters for the method you are having trouble with.

The table and/or ground properties of your Player class are likely null as I don't see code that sets them.

It's not good practice to have method parameters named the same as your class properties.

Try removing the properties in your Player class. (I don't see them being set or used)

If you still get the error, trace out their values at the beginning of the playerOneCollisions method and ensure they are not null.

If they are null, the problem exists outside this class and you are passing in a null value.

I should also note that I don't see anywhere in the Main class where you set the table or ground properties, so they are null unless you have not shown us all the code. If they are null, then that will certainly cause the issue you are experiencing.