2013-02-16 50 views
2

我已經爲一個遊戲創建了一個Player類的Javascript對象,我一直在使用下面的行從Collidable類「繼承」:繼承後無法獲得Javascript對象來構建(在對象內)

Player.prototype = new Collidable(50, 50, 70); 

這可碰撞類有一個Vector類,這是在我的代碼實例等的實例:

this.pos = new Vector(x, y) || new Vector(50, 50); 

我的問題是,我可以創造一個新的可碰撞的對象就好了,和向量裏面將有在01的前兩個參數中給出的x和y的值部分。但是,當創建新播放器時(current = new Player();),x和y的向量值將變爲NaN。

下面我已經包含了Collidable構造函數和Player構造函數的代碼。

可碰撞:

Collidable = function Collidable(x, y, d){ 
    this.pos = new Vector(x, y) || new Vector(50, 50); // Position of centre 
    this.diam = d || 50; // Diameter 
    this.col = new Color().randomRGB(); // For debug 
} 
// More functions below 

球員:

Player = function Player(){ 
    this.health = 100; 
    this.facing = 0; 
    this.sprites = new Image(); 
    this.sprites.src = "./npc-oldman1.png"; 
    this.spriteNo = 0; 
    this.moveSpeed = 2; 
} 

Player.prototype = new Collidable(50, 50, 70); 
// More functions below 

我懷疑這是關係到this的問題,但一直沒能制定出什麼錯誤。

我的完整代碼是here。它應該做的是顯示一個老人的圖像,該老人移動到鼠標點擊的位置(它在(50,50)(創建玩家的地方)處開始閃爍,或者當您手動更改pos值時) 。在添加Collisions類之前,我已經使用了代碼。

在此先感謝。

+1

'new Vector(x,y)||新的矢量(50,50);'可能不會做你想要的。我希望你想要'new Vector(x || 50,y || 50)' – Eric 2013-02-16 20:30:16

+1

更有趣:'new Vector(1,1).theta()== new Vector(-1,-1)。 theta()' – Eric 2013-02-16 20:47:30

回答

1

vector.js代碼做這個檢查:

if (typeof x === 'NaN' || typeof y === 'NaN') 

然而,typeof NaN == 'number'。你想isNaN(x),或更隱晦,x != x


修復的是,它變得很明顯,你的問題是在其他地方。這條線:

var diff = new Vector(this.getCentre.x - x, this.getCentre.y - y); 

應該之一:

var diff = new Vector(this.getCentre().x - x, this.getCentre().y - y); 
var diff = this.getCentre().diff(new Vector(x, y)) 

有缺少括號的不少套。

+0

謝謝,這個工作。我之前使用過get和set在對象符號中,當我改變它時錯過了這些。 – 2013-02-16 20:45:01

+1

這就是我所設想的。儘管@Bergi指出的東西是一個真正的問題,當你創建第二個「Collidable」時,它會咬你。 – Eric 2013-02-16 20:46:38

0

或許您的代碼存在與您所顯示的不同的問題。也許它是無序的?我無法重現NaN,這裏是我用什麼:

HTML

<div>Vector</div> 
<div><span>X: </span><span id="x"></span><div> 
<div><span>Y: </span><span id="y"></span></div> 

JS

var Vector = function(x,y){ 
this.x = x; 
this.y = y; 
} 
var Collidable = function Collidable(x, y, d){ 
    this.pos = new Vector(x, y) || new Vector(50, 50); 
} 
var Player = function Player(){ 
    this.health = 100; 
} 
Player.prototype = new Collidable(50, 50, 70); 

var current = new Player(); 

console.log(current); 
console.log(current.pos); 

document.getElementById("x").innerHTML = current.pos.x; 
document.getElementById("y").innerHTML = current.pos.y; 

演示:http://jsfiddle.net/MuRNx/

+0

不,這不是原因。 – Bergi 2013-02-16 18:58:39

+0

我很確定這不是它。如果您轉到我的頁面上的控制檯並鍵入「當前」,它會向您顯示播放器對象,併爲矢量位置指定一個pos成員。 – 2013-02-16 20:14:23

2

這個問題似乎是Inheritance issues with nested objects之間的組合reason [not] to use the new keyword/shared properties from constructor inheritance。該解決方案將

function Player(){ 
    Collidable.call(this, 50, 50, 70); // give *this* instance an *own* Vector 
    this.health = 100; 
    this.facing = 0; 
    this.sprites = new Image(); 
    this.sprites.src = "./npc-oldman1.png"; 
    this.spriteNo = 0; 
    this.moveSpeed = 2; 
} 

Player.prototype = Object.create(Collidable.prototype); // without creating instance 
+0

謝謝,但這並沒有解決它。此修補程序的版本目前在線。 – 2013-02-16 20:12:06

+0

啊,我看到埃裏克有一個解決方案。我只看了你在這裏發佈的代碼。 – Bergi 2013-02-16 21:25:10