我创建了一个Sprite类,然后从Sprite类派生出一个Dragon类。我正在尝试将操作方法添加到龙类中,但它不起作用。我以与将Sprite.prototype添加到Sprite类的方法相同的方式将操作方法添加到Dragon.prototype中,但是如果我创建了Dragon类的实例,则无法调用操作方法I在Dragon原型中定义。添加方法到原型
如果我将Dragon.prototype.action更改为Sprite.prototype.action,则代码完美工作。对于这个例子,这并不重要,但我希望能够为以后的特定类设置方法。我究竟做错了什么?我应该只是解决更多的方法名称?如果答案很明显,我表示歉意,并感谢您的帮助。
var Sprite = function(spriteSheet, rows, columns, x, y, width, height){
this.frameSpeed = 1;
this.spriteSheet = spriteSheet;
//The starting Location
this.x = x;
this.y = y;
//The width and height of the sprite
this.width = width;
this.height = height;
//Variables for movement
this.xSpeed = 0;
this.ySpeed = 0;
//rows and columns of our spritesheet
this.rows = rows;
this.columns = columns;
//our variables needed to animate the sprite.
this.sourceWidth = this.spriteSheet.width/this.columns;
this.sourceHeight = this.spriteSheet.height/this.rows;
this.sourceX = 0;
this.sourceY = 0;
this.frameCount = this.rows * this.columns;
this.currentFrame = 0;
}
Sprite.prototype.update = function(){
this.y+=this.ySpeed;
this.x+= this.xSpeed;
if(this.currentFrame >= this.frameCount){
this.currentFrame = 0;
}
this.row = Math.floor(this.currentFrame/this.columns);
this.column = Math.floor(this.currentFrame % this.columns);
this.currentFrame+=this.frameSpeed;
this.sourceX = this.column * this.sourceWidth;
this.sourceY = this.row * this.sourceHeight;
}
Sprite.prototype.draw = function(){
context.drawImage(this.spriteSheet, this.sourceX, this.sourceY, this.sourceWidth, this.sourceHeight, this.x, this.y, this.width, this.height);
}
var mySprite = new Sprite(smiley,4,4,100,50,100,100);
var Dragon = function(x, y, width, height){
var dragon = new Sprite(dragonSheet,4,3,x,y, width,height);
dragon.frameSpeed = .5;
dragon.xSpeed = 4;
return dragon;
}
Dragon.prototype.action = function(){
if(this.x>500){
this.x = -100;
}
}
var dragon1 = new Dragon(70,170,80,50);
什么问题? – Mathletics 2014-10-20 20:09:18
为什么在将Dragon.prototype.action更改为Sprite.prototype.action时不起作用? – 2014-10-20 20:12:23