1

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;
                };
3
  • Inside the World() function, you have no variables or parameters named imagePath, positionX, or positionY. So of course those are undefined. Commented Apr 25, 2015 at 22:09
  • I assumed Sprite.call would get those variables from Sprite? Commented Apr 25, 2015 at 22:15
  • No, Sprite.call() is calling into the Sprite() 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. Commented Apr 25, 2015 at 22:19

2 Answers 2

1

This is one way you could accomplish what it looks like you're trying to do:

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;
};
Sign up to request clarification or add additional context in comments.

1 Comment

So this would also inherit the imagePath?
0

You have Sprite.call(this, imagePath, positionX, positionY, direction); but the arguments to Sprite don't match this.

3 Comments

So the variables need to be in the argument for Sprite? I was hoping I didn't have to pass those for them to be inherited...
You either have to define them in World itself or pass it in to Sprite so that Sprite can define it.
And how did you pass them into Sprite?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.