Javascript - 如何使我的特定功能阻止?

时间:2014-05-29 22:07:54

标签: javascript arrays

让我感到困惑的快速查询。

执行此操作:console.dir(playerObjects);

返回:

Array[0]
0: hiderGameClient.obj.player
1: hiderGameClient.obj.player
length: 2
__proto__: Array[0]

但是当我这样做时:console.dir(playerObjects[0]);

它返回:

undefined

当预期结果应为:

0: hiderGameClient.obj.player
    displayObject: 1
    location: Array[2]
    name: "asdf"
   __proto__: Object

任何想法发生了什么?

我无法发布一个小提琴,因为它使用websockets从节点调用数据。

创建对象数组:

data.forEach(function(obj) {
    var tmp = new hiderGameClient.obj.player();
    tmp.name = obj.displayName; // Gonna actually skip this binding after it's fixed
    tmp.location = obj.pos;
    tmp.displayObject = obj.object;
    playerObjects.push(tmp);
});

编辑2:

hiderGameClient.gfx.updateGame = function() {
    playerObjects = [];
    getAllPlayerData = hiderGameClient.net.getAllPlayerData(function(data){
        data.forEach(function(obj) {
            var tmp = new hiderGameClient.obj.player();
            tmp.name = obj.displayName; // Gonna actually skip this binding after it's fixed
            tmp.location = obj.pos;
            tmp.displayObject = obj.object;
            playerObjects.push(tmp);
        });
    });
};

hiderGameClient.gfx.drawGame = function() {
    console.dir(playerObjects[0].toString());
    console.log("typeof playerObjects: " + typeof playerObjects);
    console.log("typeof playerObjects[0]: " + typeof playerObjects[0]);

    playerObjects.forEach(function(obj) {
        var canvas = document.getElementById('myCanvas');
        var context = canvas.getContext('2d');
        var imageObj = new Image();
        imageObj.onload = function() {
            context.drawImage(imageObj, 200, 300);
        };
        imageObj.src = './maps/' + map + '/obj/' + '1' + '.png';
    });
};
hiderGameClient.net.getAllPlayerData = function(callback) {
    socket.emit('whereare players', {timestamp: new Date().getTime()});
    socket.on('answer playerPositions', function(data) {
        callback(data);
    });
}

编辑3:

忘记主循环功能:

hiderGameClient.gfx.mainloop = function() {
    this.updateGame();
    this.drawGame();
};

0 个答案:

没有答案