Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建变量引用的函数的新实例_Javascript_Function_Variables_Object_Instance - Fatal编程技术网

Javascript 创建变量引用的函数的新实例

Javascript 创建变量引用的函数的新实例,javascript,function,variables,object,instance,Javascript,Function,Variables,Object,Instance,我正在尝试为HTML5画布游戏创建一种存储和加载地图的方法。为此,我必须创建变量引用的对象函数的新实例。这是我的密码 function Map(w, h) { if (typeof w == "undefined") this.width = 640; else this.width = w; if (typeof h == "undefined") this.hei

我正在尝试为HTML5画布游戏创建一种存储和加载地图的方法。为此,我必须创建变量引用的对象函数的新实例。这是我的密码

    function Map(w, h) {
        if (typeof w == "undefined")
            this.width = 640;
        else
            this.width = w;

        if (typeof h == "undefined")
            this.height = 480;
        else
            this.height = h;

        this.obj = new Array();
        this.x = new Array();
        this.y = new Array();
        this.backgroundColor = "#C0C0C0";
    }

    Map.prototype.addObject = function(cl, xx, yy) {
        this.obj.push(cl);
        this.x.push(xx);
        this.y.push(yy);
    }

    function newInstance(o, x, y) {
        this.tmp = new o(); //This is what doesn't work.
        tmp.x = x;
        tmp.y = y;
        objects.push(tmp);
        tmp.create();
    }

    Map.prototype.load = function() {
        for (var i = 0; i < this.obj.length; i++) {
            newInstance(this.obj[i], this.x[i], this.y[i]);
        }
    }
这只是一个播放器和块对象如何工作的示例

    function Player() {
        this.x = 0;
        this.y = 0;
        //Other variables irrelevant to this problem.
    }
    //Block object code

我解决了这个问题。我在发帖前忘了检查我所有的代码,所以我提供了虚假信息。我没有使用函数播放器,而是使用var Player=ObjectResource;因为我想让它扩展ObjectResource函数。我只是把它改成我发布的内容。

我解决了这个问题。我在发帖前忘了检查我所有的代码,所以我提供了虚假信息。我没有使用函数播放器,而是使用var Player=ObjectResource;因为我想让它扩展ObjectResource函数。我只需要将其更改为我发布的内容。

为什么不能:mMain.addObjectnew Player,320240?因为这将在创建地图时创建对象,而不是在加载地图时创建对象。所有地图都是在游戏开始时创建的,但只有玩家进入该地图后才会加载。将玩家更改为新玩家,并将区块更改为新区块,而不是使用新阵列,您应该像这样声明阵列[]是的,很抱歉在我发布后看到了你的//不工作代码在这里工作很好为什么不能:mMain.addObjectnew Player,320240?因为这将在创建地图时创建对象,而不是在加载地图时创建对象。所有地图都是在游戏开始时创建的,但只有玩家进入地图后才会加载。将玩家更改为新玩家,并将块更改为新块,而不是使用新数组,你应该像这样声明数组[]是的,很抱歉在我发布后看到了它。你的//代码在这里工作正常
    function Player() {
        this.x = 0;
        this.y = 0;
        //Other variables irrelevant to this problem.
    }
    //Block object code