Forested
Forested

Reputation: 353

OOP javascript object inheritance (parent/child)

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();

Upvotes: 1

Views: 257

Answers (1)

Joe
Joe

Reputation: 82654

Replace:

Character.inheritsFrom(Unit);

with:

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

See this jsFiddle Example

Upvotes: 1

Related Questions