【问题标题】:Uncaught TypeError: Object #<asteroid> has no method 'display'未捕获的类型错误:对象 #<asteroid> 没有方法“显示”
【发布时间】:2013-04-16 04:05:36
【问题描述】:

全部在一个文件中

它是 html5/javascript 画布游戏的开始

似乎每个人的独奏都更好地链接库(这是一个文件) 或在创建对象时添加新对象(我这样做了)

代码在没有对象的情况下工作正常 :/

缩写代码

html codes
codes

var tempa = new asteroid(BASIC_L, 100, 300,300,0,0,2,2,0,0);

codes

function asteroid(type, hp, x, y, z, r, vx, vy, vz, vr)
            {
              this.type = type;
              this.hp = hp;
              this.x = x;
              this.y = y;
              this.z = z;
              this.r = r;
              this.vx = vx;
              this.vy = vy;
              this.vz = vz;
              this.vr = vr;

                function display(){
                  this.x += this.vx;
                  this.y += this.vy;

                  if(this.type == BASIC_L){
                debug(this.type);
                ctx.beginPath();
                ctx.strokeStyle="#00fff0";
                ctx.arc(this.x,this.y,100,0,2*Math.PI);
                ctx.stroke();
                  }
                }
            }

//codes 

function draw(){
   //codes
   tempa.display();
}

//codes
//html codes

【问题讨论】:

    标签: javascript html google-chrome object canvas


    【解决方案1】:

    您将display 创建为闭包方法而不是成员函数。

    function asteroid(type, hp, x, y, z, r, vx, vy, vz, vr) {
        this.type = type;
        this.hp = hp;
        this.x = x;
        this.y = y;
        this.z = z;
        this.r = r;
        this.vx = vx;
        this.vy = vy;
        this.vz = vz;
        this.vr = vr;
    
        this.display = function() {
            this.x += this.vx;
            this.y += this.vy;
    
            if (this.type == BASIC_L) {
                debug(this.type);
                ctx.beginPath();
                ctx.strokeStyle = "#00fff0";
                ctx.arc(this.x, this.y, 100, 0, 2 * Math.PI);
                ctx.stroke();
            }
        }
    }
    
    // codes
    
    function draw() {
        // codes
        tempa.display();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多