0

TypeError:错误 #2007:参数 hitTestObject 必须为非空。

我现在正在开发一款游戏并试图让它与地面发生碰撞(他已经与桌子发生碰撞)

它说问题发生在 playerOneCollisions

table 和 ground 只是将基本类代码链接到movieclip 的类,它们是扩展的movieclip(如果重要的话),并且它们已经导入了stage 和movieclip(再次,如果重要的话)

(我认为重要的部分)

播放器.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";
            }
        }

函数 playerOneCollisions 在 Main.as 中被调用

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

其余的代码以防我对重要部分有误

播放器.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);
            }
        }
    }
}

主文件

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();
        }   
    }
}
4

1 回答 1

0

我注意到的一件事是,您拥有tableground作为 的类属性Player,它们也是您遇到问题的方法的参数。

您的 Player 类的table和/或ground属性可能为 null,因为我没有看到设置它们的代码。

将方法参数命名为与类属性相同的名称并不是一个好习惯。

尝试删除 Player 类中的属性。(我没有看到它们被设置或使用)

如果您仍然收到错误,请在方法的开头跟踪它们的值playerOneCollisions并确保它们不为空。

如果它们为空,则问题存在于此类之外,并且您传入的是空值。

我还应该注意,我在 Main 类中看不到您设置tableorground属性的任何地方,因此它们为 null ,除非您没有向我们展示所有代码。如果它们为空,那么这肯定会导致您遇到的问题。

于 2013-09-19T21:03:29.800 回答