TypeError:错误#2007:参数hitTestObject必须为非null。
我现在正在制作一款游戏并试图让游戏与地面相撞(他已经与桌子发生碰撞)
它说问题发生在playerOneCollisions
表格和地面只是具有链接到动画片段的基本类代码的类, 它们是扩展的动画片段(如果重要的话),并且它们已经导入了舞台和动画片段(再次,如果这很重要)
(我认为重要的部分)
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";
}
}
在Main.as中调用playerOneCollisions函数
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
其余的代码包含了我对重要部分的错误
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();
}
}
}
答案 0 :(得分:0)
我注意到的一件事是table
和ground
作为Player
的类属性,它们也是您遇到问题的方法的参数。
您的Player类的table
和/或ground
属性可能为null,因为我没有看到设置它们的代码。
将方法参数命名为与您的类属性相同是不好的做法。
尝试删除Player类中的属性。 (我没有看到它们被设置或使用)
如果仍然出现错误,请在playerOneCollisions
方法的开头跟踪其值,并确保它们不为空。
如果它们为null,则问题存在于此类之外,并且您传入的是空值。
我还应该注意,我没有在Main类中看到您设置table
或ground
属性的任何位置,因此除非您没有向我们展示所有代码,否则它们都是null。如果它们为空,那肯定会导致您遇到的问题。