Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 HTML画布中的重叠精灵_Javascript_Html_Html5 Canvas - Fatal编程技术网

Javascript HTML画布中的重叠精灵

Javascript HTML画布中的重叠精灵,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,我找到了这个,它在画布上运行 它们如何能在同一位置上有2个字符(在1个画布中) 他们可能每秒都在更新游戏角色,如下所示: context.clearRect(0, 0, width, height); for (i=0;i<players.lenght;i++){ context.drawImage(player, player[i][x], player[i][y]); } context.clearRect(0,0,宽度,高度); 对于(i=0;i仅仅因为玩家可能在同一个游戏中

我找到了这个,它在画布上运行

它们如何能在同一位置上有2个字符(在1个画布中)

他们可能每秒都在更新游戏角色,如下所示:

context.clearRect(0, 0, width, height);
for (i=0;i<players.lenght;i++){
   context.drawImage(player, player[i][x], player[i][y]);
}
context.clearRect(0,0,宽度,高度);

对于(i=0;i仅仅因为玩家可能在同一个游戏中,并不意味着他们看到相同的视图:)(想想你在网上玩过的任何FPS游戏)

然而,你的假设是正确的。我添加到示例代码中的代码只是将您的玩家添加为数组中的最后一个玩家,这意味着您将始终显示为游戏中的顶级玩家(无论是2名还是10名玩家)

context.clearRect(0,0,宽度,高度);
//把你的球员移到顶端
对于(i=0;i
context.clearRect(0, 0, width, height);

// Move your player to the top
for (i=0;i<players.lenght;i++){
    if (players.isYou()) {
        var you = players.splice(i, 1);
        players.push(you);
    }
}

for (i=0;i<players.lenght;i++){
    context.drawImage(player, player[i][x], player[i][y]);
}