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();
            }

        }

    }
13
  • 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 Commented May 9, 2013 at 7: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 :) Commented May 9, 2013 at 7:48
  • Then you will need #1. And maybe also have #2 if you don't want that array to be accessed (and manipulated?) directly Commented May 9, 2013 at 7: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? Commented May 9, 2013 at 7:54
  • 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. Commented May 9, 2013 at 8:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.