AS3错误1061试图发生碰撞

时间:2013-09-04 03:17:34

标签: actionscript-3

“Player.as,第59行1061:通过带有静态类型Class的引用调用可能未定义的方法hitTestObject。”

我是flash的新手并且我正在尝试制作一款游戏,特别是我试图让游戏中的玩家级别与事物发生碰撞,我在他的脚下使用了一个正方形(尚未引用)在代码中)和一个名为collisionTest的MovieClip

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

    public class Player extends MovieClip
    {
        public var stageRef:Stage;
        public var key:KeyObject;

        //add these four variables:
        public var leftPressed:Boolean = false; //keeps track of whether the left arrow          key is pressed
        public var rightPressed:Boolean = false; //same, but for right key pressed
        public var upPressed:Boolean = false; //...up key pressed
        public var downPressed:Boolean = false; //...down key pressed

        private var gravity:Number = 2;
        private var runSpeed:Number = 5;

        private var touchingGround:Boolean = false;

        public var vPressed:Boolean = false;

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

            key = new KeyObject(stageRef);

            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        }

        public function loop(e:Event):void
        {
            checkKeypresses(); //call "checkKeypresses()" every frame
            checkCollisions();

            if(leftPressed)
            {
                x -= runSpeed;
            }else if(rightPressed)
            {
                x += runSpeed;
            }
            if(upPressed)
            {
                y -= runSpeed;
            }else if(downPressed)
            {
                y += runSpeed;
            }
        }
        public function checkCollisions():void
        {
**(this is line 59)**           if(Player.hitTestObject(Player.collisionTest)){
                touchingGround = true;
                trace("gounded");
            }
        }

        public function checkKeypresses():void
        {
            // I used http://www.dakmm.com/?p=272 as a reference to get the keyCode numbers for each key
            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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

函数hitTestObject不是静态函数,因此您应该从对象实例调用它,与collisionTest相同。

所以它应该是

this.hitTestObject(collisionTest);//set collisionTest in the Player class