2013-04-23 37 views
0

一切都在一个文件中遗漏的类型错误:对象#<asteroid>有没有方法“显示”

一个HTML5的它的开始/ JavaScript的帆布游戏

似乎每个人solotion更好的连接库(这是所有一个文件) 或创建对象(我做了),当添加新

的代码工作正常,没有物体 :/

缩写代码

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 

回答

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(); 
} 
+0

是的......那工作......谢谢 – hutty 2013-04-23 21:35:01

相关问题