2015-06-24 43 views
0

的「X」,我得到一個錯誤:無法讀取屬性未定義的JavaScript

Uncaught TypeError: Cannot read property 'x' of undefined

我試圖使物體移動,但是,試圖利用Vector2當我得到這個問題。這裏是我的代碼:

Player.js

var bullets = []; 
var LEFT = 0; 
var RIGHT = 1; 
var UP = 2; 
var DOWN = 3; 

var Player = function(){ 
this.sprite = new Sprite("playerplaceholder.png"); 
this.sprite.buildAnimation(1, 1, 32, 32, -1, [0]); 
this.sprite.setAnimationOffset(0, 0, 0); 
this.sprite.setLoop(0, false); 

this.position = new Vector2(); 
this.position.set(200, 200); 

this.width = 48 
this.height = 48 

this.velocity = new Vector2(); 

this.moveVector = new Vector2(); 
this.moveVector.set(this.position.x, this.position.y); 
}; 

Player.prototype.update = function(deltaTime) 
{ 
this.sprite.update(deltaTime); 

if((keyboard.isKeyDown(keyboard.KEY_LEFT)) != (keyboard.isKeyDown(keyboard.KEY_RIGHT))) 
{ 
    if(keyboard.isKeyDown(keyboard.KEY_RIGHT)) 
    { 
     Player.moveVector.x += 1; 
    } 
    else 
    { 
     Player.moveVector.x += -1; 
    } 
} 
if((keyboard.isKeyDown(keyboard.KEY_UP)) != (keyboard.isKeyDown(keyboard.KEY_UP))) 
{ 
    if(keyboard.isKeyDown(keyboard.KEY_DOWN)) 
    { 
     Player.moveVector += -1; 
    } 
    else 
    { 
     Player.moveVector += 1; 
    } 
} 

Player.position += Player.moveVector; 

} 

Player.prototype.draw = function() 
{ 
this.sprite.draw(context, this.position.x, this.position.y); 
} 

Vector2

var Vector2 = function() 
{ 
this.x = 0; 
this.y = 0; 
}; 

Vector2.prototype.set = function(x,y) 
{ 
this.x = x; 
this.y = y; 
}; 

Vector2.prototype.add = function(a_x,a_y) 
{ 
    return (this.x + a_x),(this.y + a_y); 
} 

Vector2.prototype.subtract = function(a_x,a_y) 
{ 
    return (this.x - a_x),(this.y - a_y); 
} 

Vector2.prototype.MultiplyScalar = function(input) 
{ 
return (this.x * input),(this.y * input); 
} 

Vector2.prototype.Magnitude = function() 
{ 

return Math.sqrt((x*x) + (y*y)) //Gives magnitude of current vector2 
} 

Vector2.prototype.Normalize = function() 
{ 

var rect = new Vector2(); 
rect.x = this.x/this.Magnitude(); 
rect.y = this.y/this.Magnitude(); 

return ret; 
} 
+0

剛纔忘了提及的是,錯誤是在其上線33: Player.moveVector.x + = 1; – Whipgun

+0

'Player.moveVector'未定義。嘗試'console.log(Player);'在第32行並檢查控制檯輸出。此外,在任何時候你都沒有指派'moveVector'屬性給'Player' – atmd

回答

0

也許這個錯誤是因爲你試圖訪問moveVector功能失常。

Player.moveVector.x += 1; 

這裏你就Player功能是undefined訪問moveVector。也許你想要的是這樣的:

this.moveVector.x += 1; 

同樣爲其他在Player.prototype.update

Player.prototype.update = function(deltaTime) 
{ 
    this.sprite.update(deltaTime); 

    if((keyboard.isKeyDown(keyboard.KEY_LEFT)) != (keyboard.isKeyDown(keyboard.KEY_RIGHT))) 
    { 
     if(keyboard.isKeyDown(keyboard.KEY_RIGHT)) 
     { 
      this.moveVector.x += 1; 
     } 
     else 
     { 
      this.moveVector.x += -1; 
     } 
    } 
    if((keyboard.isKeyDown(keyboard.KEY_UP)) != (keyboard.isKeyDown(keyboard.KEY_UP))) 
    { 
     if(keyboard.isKeyDown(keyboard.KEY_DOWN)) 
     { 
      this.moveVector += -1; 
     } 
     else 
     { 
      this.moveVector += 1; 
     } 
    } 

    this.position += this.moveVector; 

} 
+0

謝謝,但是在試圖確認屏幕在控制檯中沒有任何錯誤的情況下是空白的。 – Whipgun

相關問題