JavaScript 继承 Object.call() 未定义
JavaScript Inheritance Object.call() undefined
我正在尝试为我的程序创建一个面向对象的方法。我读到这应该从 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;
};
您有 Sprite.call(this, imagePath, positionX, positionY, direction);
,但 Sprite
的参数与此不匹配。
这是您可以完成您正在尝试做的事情的一种方法:
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;
};
我正在尝试为我的程序创建一个面向对象的方法。我读到这应该从 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;
};
您有 Sprite.call(this, imagePath, positionX, positionY, direction);
,但 Sprite
的参数与此不匹配。
这是您可以完成您正在尝试做的事情的一种方法:
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;
};