【问题标题】:Undefined hitTestPoint error未定义的 hitTestPoint 错误
【发布时间】:2013-10-22 08:28:37
【问题描述】:

当我有这段代码时,它会出现这个错误“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 变量,这确实消除了这个错误,但我不确定如何将我的实际平台实例链接到我的变量。哦,顺便说一下平台的实例是平台。如果有人对如何修复此错误有任何线索,那就太好了。

【问题讨论】:

  • 我希望您平台的类名也是platform,尝试正确使用大写类名-实例名为platform,AS3 链接为Platform

标签: actionscript-3 function undefined movie clip


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2012-08-09
    相关资源
    最近更新 更多