未定义的hitTestPoint错误

时间:2013-10-22 08:28:38

标签: actionscript-3 function undefined movie clip

当我有这个代码时,它会出现这个错误“1061:通过带有静态类型Class的引用调用可能未定义的方法hitTestPoint。”

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;

public class Code extends MovieClip {

    var charSpeed:int = 0;
    var velocity:int = 0;
    var gravity:Number = 1;
    var Jump:Boolean = false;


    public function startGame(){
         stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
         stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
         stage.addEventListener(Event.ENTER_FRAME, loop);
     }

    public function Code() {
        // constructor code
    }

    function checkKeyDown(evt:KeyboardEvent){
        if (evt.keyCode == Keyboard.LEFT){
            charSpeed -= 10;
        }
        if (evt.keyCode == Keyboard.RIGHT){
            charSpeed += 10;
        }
        if (evt.keyCode == Keyboard.DOWN){
            if(!Jump){
                velocity -= 14;
                Jump = true;
            }
        }
    }

    function checkKeyUp(evt:KeyboardEvent){
        if (evt.keyCode == Keyboard.LEFT){
            charSpeed = 0;
        }
        if (evt.keyCode == Keyboard.RIGHT){
            charSpeed = 0;
        }
    }

    function loop(evt:Event){
        player.x = velocity;
        if (player.x < 0){
             player.x = 0;
        }
        if (player.x > 550){
             player.x = 550;
        }

        velocity += gravity;

        if (!platform.hitTestPoint(player.x, player.y, true)){
            player.y += velocity;
        }

        for (var i = 0; i < 10; i++){
            if (platform.hitTestPoint(player.x, player.y, true)){
                player.y--;
                velocity = 0;
                Jump = false;
            }
        }
    }
}
}

我的选项是将一个MovieClip变量添加到我的平台,这确实消除了这个错误,但我不知道如何将我的实际平台实例链接到我的变量。哦,顺便说一下平台的实例是平台。如果有人有任何关于如何解决这个错误的线索,那就太好了。

1 个答案:

答案 0 :(得分:0)

您可以在构造函数中设置平台值

public function class Code {

  //assume Platform is the class type,remember to import it
   private var platform:Platform;

   public function Code(value:Platform) {
       platform = value;
   }

}

创建代码实例

var code:Code = new Code(platform);//platform is the instance