0

我正在研究我作为学校作业收到的一个小框架……我选择用 JavaScript 做一些事情……这是我到目前为止的代码:

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

})();

我稍微使用了本教程来了解如何开始: http: //www.admixweb.com/2009/05/20/how-to-easily-create-a-javascript-framework-part-1/

无论如何......我的问题是:我想要一种方法,以便使用我的框架的人可以添加多个玩家(具有不同的设置),如果他喜欢......据我所知,可能有两种理论方法(我说理论,因为我不知道它们在当前上下文中是否可行):

  1. 以某种方式在我的框架中拥有一系列玩家
  2. 拥有某种从框架返回玩家的吸气剂......并且该玩家可能存储在框架外的数组中......

你们怎么看..什么方法更好或可行?

谢谢

编辑:添加了一个存储玩家数组的字段。该字段与框架中的玩家处于同一级别。下面的代码:

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

        }

    }
4

0 回答 0