我已经为一个游戏创建了一个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