我正在尝试为我的程序创建面向对象的方法。我读到这应该从Sprite创建一个World的继承,Sprite是父,但是Sprite.call(this,imagePath)出现,因为imagePath是未定义的。我会假设调用中的其他变量也是未定义的。如何正确调用父变量?
function Sprite(spriteX, spriteY, spriteW, spriteH, scale, positionX, positionY, direction)
{
this.imagePath = world_sprite;
this.spriteX = spriteX;
this.spriteY = spriteY;
this.spriteW = spriteW;
this.spriteH = spriteH;
this.scale = scale;
this.positionX = positionX;
this.positionY = positionY;
this.direction = direction;
this.speed = 5;
this.noGravity = false;
this.direction = 0;
//Physics stuff
this.velX = 0;
this.velY = 0;
this.friction = 0.98;
};
function World(posX, posY, direction, xOffset, yOffset)
{
Sprite.call(this, imagePath, positionX, positionY, direction);
this.spriteX = 0;
this.spriteY = 0;
this.spriteW = 400;
this.spriteH = 400;
this.scale = 0.4;
this.xOffset = xOffset;
this.yOffset = yOffset;
this.lives = 3;
};
答案 0 :(得分:1)
这是你可以完成你想要做的事情的一种方式:
function Sprite(
spriteX, spriteY, spriteW, spriteH,
scale, positionX, positionY, direction
) {
this.imagePath = world_sprite;
this.spriteX = spriteX;
this.spriteY = spriteY;
this.spriteW = spriteW;
this.spriteH = spriteH;
this.scale = scale;
this.positionX = positionX;
this.positionY = positionY;
this.direction = direction;
this.speed = 5;
this.noGravity = false;
this.direction = 0;
//Physics stuff
this.velX = 0;
this.velY = 0;
this.friction = 0.98;
};
function World(
posX, posY, direction, xOffset, yOffset
) {
Sprite.call( this, 0, 0, 400, 400, 0.4, posX, posY, direction );
this.xOffset = xOffset;
this.yOffset = yOffset;
this.lives = 3;
};
答案 1 :(得分:0)
您有Sprite.call(this, imagePath, positionX, positionY, direction);
,但Sprite
的参数与此不匹配。