javascript - RequireJS and Class Inheritance -


i'm trying implement classical inheritance in javascript using requirejs modules build game engine using html5. i've tried using john resig's class.js doesn't seem work way expected. here github rep project, app can run w/o server if download files , open index.html in client folder.

in app create base class called entity. entity base class objects in game , extended number of other classes. sprite class extends entity class , includes animation methods. player class extends sprite class.

the issue appeared when created class named image extend entity class. both player , image classes extend entity (player goes entity->sprite->player), when player sets velocity effects instances of images? added console.log prints objects velocity in image's think function (game loop tick). if press arrow keys make player walk around see player's velocity coming image think print.

shouldn't independent objects individual attributes? that's i'm going for. ideas on i'm doing wrong or link/pattern accomplish proper inheritance?

entity#velocity should initialized in constructor rather in "class definition" (although, there no such thing in javascript, helper libraries such john resig's class.js).

all javascript variables , properties merely pointers different location in memory. code changing velocity property's x property, without changing reference of velocity, , hence why change in property x mirrored in other objects.

so instead of:

var entity = class.extend({   velocity: { x: 0, y: 0 },   init: function () { } }); 

you do:

var entity = class.extend({   init: function () {     this.velocity = { x: 0, y: 0 };   } }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -