I am attempting to create an object-oriented approach to my program. I read that this should create an inheritance of World from Sprite with Sprite being the parent, but Sprite.call(this, imagePath) comes up as imagePath is undefined. I would assume the other variables in the call would also be undefined. How do I appropriately call the parent variables?
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;
};
World()function, you have no variables or parameters namedimagePath,positionX, orpositionY. So of course those are undefined.Sprite.call()is calling into theSprite()function. At the point where you make the call, there is no knowledge of what goes on inside that function. You have to use only what you have available at that point. See my answer below for one way you could do this.