1

Hello I'm working with Javascript OOP and I can't get it to work. I want to call functions from another object (parent) via the child. This is my "class" code:

function Unit(_x, _y, _speed) {
    this.x = _x;
    this.y = _y;
    this.dx = _speed;
    this.dy = _speed;
    this.gravity = 0;
}
Unit.prototype.apply_gravity = function() {
    this.gravity+=global_gravity;
    this.y+=this.gravity;
}
Unit.prototype.render = function() {
    context.drawImage(images[0], canvas_screen.x+this.x, canvas_screen.y+this.y);
}

function Character() {
}

Character.inheritsFrom(Unit);

I want to be able to call the functions: "apply_gravity" and "render" from the character object like this:

var character = new Character(100,100,5);
character.render();

1 Answer 1

1

Replace:

Character.inheritsFrom(Unit);

with:

Character.prototype = new Unit(0,0,0); // or whatever defaults if you want them

See this jsFiddle Example

Sign up to request clarification or add additional context in comments.

1 Comment

I imagine inheritsFrom is some 3rd party library (and perhaps used incorrectly if it's Type.inheritsFrom), although I have no urge to investigate further.

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.