I am working on a small framewok that i received as a school assignment...and I chose to do someting in JavaScript...this is the code that I have until now :
(function (){
//Creating our framework named GM (from game)
var GM = {
canvas : null, // the place where all the action happens is the canvas
context : null,
// other functions ...
player : {
frameNr : 0,
frames : [],
loaded : new Array(),
loadedImages : false,
xCoord : 0, // current X coordinate of the player
yCoord : 0, // current Y coordinate of the player
speed : 0, // speed of the moving player
xDirection : 0,
yDirection : 0,
width : 0,
height : 0,
// other things related to player
}
}
if(!window.$$){window.$$=GM;}//We create a shortcut for our framework, so we can call the methods by $$.method();
})();
I used this tutorial a little bit to see how to get started : http://www.admixweb.com/2009/05/20/how-to-easily-create-a-javascript-framework-part-1/
Anyway...my question is this : I want a way so that whoever is using my framework can add multiple players(with different settings) if he likes...From what i can tell there could be 2 theoretical approaches (i say theoretical, because i don't know if they are doable or not given the current context):
- To somehow have an array of players in my framework
- To have some kind of getter that returns a player from the framework....and that player is maybe stored in an array outside the framework...
What do you guys think..what approach is better or doable ?
Thanks
EDIT: Added a field which stores the array of players. This field is on the same level as player in the framework. Code below :
players : {
array : [],
length : 0,
add : function(player){
this.array.push(player);
this.length = this.length + 1;
console.log("+ 1 player");
},
animate : function(){
for (i=0;i<this.length;i++){
this.array[i].animate();
}
}
}