oop - JavaScript Inheritance Object.call() undefined -
i attempting create object-oriented approach program. read should create inheritance of world sprite sprite being parent, sprite.call(this, imagepath) comes imagepath undefined. assume other variables in call undefined. how appropriately call 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; };
this 1 way accomplish looks you're trying 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; };
Comments
Post a Comment