我已經爲一個遊戲創建了一個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類之前,我已經使用了代碼。
在此先感謝。
'new Vector(x,y)||新的矢量(50,50);'可能不會做你想要的。我希望你想要'new Vector(x || 50,y || 50)' – Eric 2013-02-16 20:30:16
更有趣:'new Vector(1,1).theta()== new Vector(-1,-1)。 theta()' – Eric 2013-02-16 20:47:30