Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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/6/multithreading/4.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_Html_Html5 Canvas - Fatal编程技术网

Javascript 为敌人重生而循环

Javascript 为敌人重生而循环,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,在游戏中使用敌人重生功能,但是我的for循环似乎没有效果,因为我仍然只有一个ememy在屏幕上移动,谢谢 Monster.prototype.draw = function () { for (var i=0; i <5; i++){ clearMonster(); monsterCon.drawImage(sprite, this.locationX, this.locationY, this.width, this.heigh

在游戏中使用敌人重生功能,但是我的for循环似乎没有效果,因为我仍然只有一个ememy在屏幕上移动,谢谢

Monster.prototype.draw = function () {      
        for (var i=0; i <5; i++){
        clearMonster();
        monsterCon.drawImage(sprite, this.locationX, this.locationY, this.width, this.height, this.destinationX, this.destinationY, this.width, this.height);
                    this.checkKeys();
        }
    }; 
Monster.prototype.draw=function(){

对于(var i=0;i在再次添加怪物之前,您正在循环的每个迭代中清除怪物。

clearMonster();
是否正在清除怪物?clearMonster()函数只是确保每次都重新绘制画布,以便怪物不会在自己身上留下痕迹。将函数调用移到循环之外?在绘制第五个也是唯一一个怪物之前,您似乎正在擦除第五个循环运行中最后绘制的四个怪物。@user2317489在绘制每个怪物之前,整个画布都会被擦除吗你有理由只看到一个。