2013-02-16 51 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