Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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有一个bug_Javascript - Fatal编程技术网

对于一个简单的游戏,我的javascript有一个bug

对于一个简单的游戏,我的javascript有一个bug,javascript,Javascript,我遇到的错误是,当我制作用于检测玩家和使用for语句绘制的方块之间碰撞的碰撞检测系统时,它会很好地拾取碰撞。我知道这一点,因为每当它检测到碰撞以及碰撞发生的一侧时,我都会将其记录到控制台。但是在控制台日志之后,它应该通过设置一个变量来停止玩家的移动,我正在使用该变量将移动范围从10设置为0,但事实并非如此,很抱歉添加了所有代码,我只是不知道为什么会出现问题 var frame=setInterval(图10); var canvas=document.getElementById('canva

我遇到的错误是,当我制作用于检测玩家和使用for语句绘制的方块之间碰撞的碰撞检测系统时,它会很好地拾取碰撞。我知道这一点,因为每当它检测到碰撞以及碰撞发生的一侧时,我都会将其记录到控制台。但是在控制台日志之后,它应该通过设置一个变量来停止玩家的移动,我正在使用该变量将移动范围从10设置为0,但事实并非如此,很抱歉添加了所有代码,我只是不知道为什么会出现问题

var frame=setInterval(图10);
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext('2d');
var pos=[[300250]、[500250]、[700250]]
var高度=150;
var宽度=150;
变量plyr={pos:[0,0],宽度:50,高度:50};
var a={up:10,down:10,left:10,right:10};
函数绘图(){
canvas.width=canvas.width
对于(变量i=0;ix2&&x2+width2>x1&&y1+height1>y2&&
y2+高度2>y1){
如果(y1+高度1>y2&&y2+20>y1+高度1)
{console.log('touch-down');a.down=0;}
否则如果(y2+高度2>y1和&y1+20>y2+高度2)
{console.log('touch');a.up=0;}
否则如果(x1+width1>x2&&x2+20>x1+width1)
{console.log('touch right');a.right=0;}
否则如果(x2+width2>x1&&x1+20>x2+width2)
{console.log('touch left');a.left=0;}
}
否则{
a、 up=10;
a、 向下=10;
a、 左=10;
a、 右=10;
}
}

测试

也许解决这个问题的更好方法是将玩家的位置设置为碰撞时对象的边缘

      function touch(x1,y1,width1,height1,x2,y2,width2,height2){
        if( x1 + width1 > x2 && x2 + width2 > x1 && y1 + height1 > y2 && 
        y2 + height2 > y1){
          if( y1 + height1 > y2 && y2 + 20 > y1 + height1 )
          {console.log('touch down');plyr.pos[1] = y2-plyr.height;}
          else if( y2 + height2 > y1 && y1 + 20 > y2 + height2)
          {console.log('touch up');plyr.pos[1] = y2+height2;}
          else if( x1 + width1 > x2 && x2 + 20 > x1 + width1)
          {console.log('touch right');plyr.pos[0] = x2-plyr.width;}
          else if( x2 + width2 > x1 && x1 + 20 > x2 + width2)
          {console.log('touch left');plyr.pos[0] = x2+plyr.width;}
        }
      }

这么多条件语句。您应该尝试重新编写代码,使其更清晰易读。这是我第一次真正尝试使用java脚本,因此,如果您能建议另一种方法,我们将不胜感激