0

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):

  1. To somehow have an array of players in my framework
  2. 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();
            }

        }

    }
Teshte
  • 624
  • 1
  • 7
  • 26
  • Depends whether your game logic needs to know about all created players. Then you won't get around using an internal `players` array even with option #2 – Bergi May 09 '13 at 07:47
  • well yes..it would need to keep track of all players..and each of them might have different properties.. this is kind of the point :) – Teshte May 09 '13 at 07:48
  • Then you will need #1. And maybe also have #2 if you don't want that array to be accessed (and manipulated?) directly – Bergi May 09 '13 at 07:50
  • I tried something like `x = $$.player.get(); $$.players.add(x); $$.player.setPosition(500,200); y = new $$.player(); y = $$.player.get(); $$.players.add(y);` where player.get has the following code `get : function(){ return this; }` The problem is...that in my array both objects have the same properties...Why is that and how do i solve it? – Teshte May 09 '13 at 07:54
  • What is `$$.players`? Please put your code [in the question](http://stackoverflow.com/posts/16456008/edit) (where you can format it). – Bergi May 09 '13 at 08:00
  • added the code...basically it is a field that has an array of player – Teshte May 09 '13 at 08:02
  • Are you using `new $$.player` with the above object literal definition of player? If you use `new`, it needs to be a constructor function - please show that. – Bergi May 09 '13 at 08:05
  • I tried `x = new $$.player();` but i get the error `is not a constructor` ...how can i write that constructor function? – Teshte May 09 '13 at 08:06
  • http://stackoverflow.com/q/1114024/1048572 is the first search result… – Bergi May 09 '13 at 08:14
  • so basically i have to make a function out of player...and have all the fields from it like `this.field`...? I kind of understood th example from the link you provided...not sure if this is the way to apply it to my situation – Teshte May 09 '13 at 08:24
  • What i mean is that `player` also contains functions not only the fields defined in the code from above...can i still use those functions ? Because now i get something like `$$.player.setFrames is not a function` – Teshte May 09 '13 at 08:33
  • 1
    Yes, it is also possible to create properties on a function object. But you will need to manually assign them, you cannot declare them in literal syntax. – Bergi May 09 '13 at 08:43
  • Thanks ..i finally made it – Teshte May 09 '13 at 13:27

0 Answers0