未捕获的TypeError:对象#<asteroid>没有方法'display'</asteroid>

时间:2013-04-23 04:24:06

标签: javascript html5 google-chrome object canvas

将其全部放在一个文件中

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

似乎每个人都能更好地链接库(这是一个文件) 或者在创建对象时添加new(我做了)

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

缩写代码

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

1 个答案:

答案 0 :(得分:0)

您创建了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();
}