jeudi 5 février 2015

Subclass of a Subclass?


I've been writing a game engine, and I wanted to re-organize my code to make it more modular.


Currently, I have a the main function called 'Isometric' that accepts the canvas to draw.



var iso = new Isometric('canvas');


So far so good. Next, I had .newMap() would you create a new map.



var map = iso.newMap(10,1,10); // Creates 10x1x10 map


However, I want to change that since there might be some confusion with the .map property (since this returns an array called map).


I wanted the syntax to look like:



iso.Map.create(10,1,10);


So I tried something like this:



function Isometric(id) {
this.Map = function() {
this.create = function() {
}
}
}


But when I went to access it, I realized that the second level of 'this' still refers to the same first level this. So, I can't create a sub-class of the Map object.


I've looked at a few different methods, but none of them had clear examples and I couldn't get them to work.


Among them, I'm aware that you can use .prototype, and Object.create() but I've not gotten much success.


How can I do this?


The other solution I have is to do something like



function Isometric('id') {
this.Map = {
'create': function() { },
'load': function() {}
}
}


and then access it like



iso.Map['create'];


but I don't like that at all. Any clean methods of doing it?


My main interest is an example with the third-level method ..create() inside .map. If you could provide me with documentation related to my question that I have not yet found, that would be nice. But even mozilla docs didn't seem to help.





Aucun commentaire:

Enregistrer un commentaire