Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 为什么我的代码落后2个刻度(而不是1个)?_Javascript_Canvas - Fatal编程技术网

Javascript 为什么我的代码落后2个刻度(而不是1个)?

Javascript 为什么我的代码落后2个刻度(而不是1个)?,javascript,canvas,Javascript,Canvas,请原谅间隔不当 Player = { move: function(cycle, opponent) { switch(cycle.current_direction) { case 'up': cycle.y -= cycle.height; break; case 'down': cycle.y += cycle.height; break; case 'right':

请原谅间隔不当

Player = {
    move: function(cycle, opponent) {
      switch(cycle.current_direction) {
      case 'up':
        cycle.y -= cycle.height;
        break;
      case 'down':
        cycle.y += cycle.height;
        break;
      case 'right':
        cycle.x += cycle.width;
        break;
      case 'left':
        cycle.x -= cycle.width;
        break;
    }
    if (this.checkCollision(cycle, opponent)) {
      Game.stop(cycle);
    }
    coords = cycle.x + ',' + cycle.y;

    if(cycle.history.length > 0){
        var prev_position = cycle.history[cycle.history.length - 1]
        var pp_split = prev_position.split(',');
        var pp_x = pp_split[0]
        var pp_y = pp_split[1]

        var coords_split = coords.split(',')
        var x = coords_split[0]
        var y = coords_split[1]

        if(parseInt(x) == (parseInt(pp_x) - 4)){
            for(i=x;i>x-4;i--){
                cycle.history.push(i+','+y)
            }
        }else if (parseInt(x) == (parseInt(pp_x) + 4)){
            for(i=x;i<x+4;i++){
                cycle.history.push(i+','+y)
            }
        }else if (parseInt(y) == (parseInt(pp_y) - 4)){
            for(i=y;i>y-4;i--){
                cycle.history.push(x+','+i)
            }
        }else if (parseInt(y) == (parseInt(pp_y) + 4)){
            for(i=y;i<y+4;i++){
                cycle.history.push(x+','+i)
            }
        }else{
            console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ERROR')
            console.log('cycle.history.length - 1 :' + (cycle.history.length - 1))
            console.log('x: ' + x, 'pp_x: ' + pp_x)
            console.log('y: ' + y, 'pp_y: ' + pp_y)
        }
    }else{
        cycle.history.push(coords);
    }    
},
Player={
移动:功能(循环,对手){
开关(循环电流方向){
案例“向上”:
cycle.y-=cycle.height;
打破
案例“关闭”:
cycle.y+=cycle.height;
打破
案例“正确”:
cycle.x+=cycle.width;
打破
案例“左”:
cycle.x-=cycle.width;
打破
}
如果(本次检查碰撞(循环,对手)){
游戏。停止(循环);
}
coords=cycle.x+','+cycle.y;
如果(cycle.history.length>0){
var prev_位置=周期.历史[周期.历史.长度-1]
var pp_split=上一个位置。split(',');
var pp_x=pp_分割[0]
var pp_y=pp_分割[1]
var coords_split=coords.split(',')
var x=坐标分割[0]
变量y=坐标和分割[1]
if(parseInt(x)=(parseInt(pp_x)-4)){
对于(i=x;i>x-4;i--){
cycle.history.push(i+','+y)
}
}else if(parseInt(x)=(parseInt(pp_x)+4)){
对于(i=x;iy-4;i--){
cycle.history.push(x+','+i)
}
}else if(parseInt(y)=(parseInt(pp_y)+4)){

对于(i=y;i,控制台似乎只记录其父条件块中的最终嵌套条件else块。在此条件块中,没有推送到history属性的数据

屏幕截图中显示为调用的唯一块:

console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ERROR')
console.log('cycle.history.length - 1 :' + (cycle.history.length - 1))
console.log('x: ' + x, 'pp_x: ' + pp_x)
console.log('y: ' + y, 'pp_y: ' + pp_y)

此块中没有推送到循环中的数据。历史记录在此块中。

控制台似乎只记录其父条件块中的最终嵌套条件else块。在此条件块中,没有推送到历史记录属性的数据

屏幕截图中显示为调用的唯一块:

console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ERROR')
console.log('cycle.history.length - 1 :' + (cycle.history.length - 1))
console.log('x: ' + x, 'pp_x: ' + pp_x)
console.log('y: ' + y, 'pp_y: ' + pp_y)
此块中没有推送到
循环的数据。历史记录

请提供一个。当前未知数据太多,我们无法帮助您。此
循环
对象是如何初始化的?它的
y
x
宽度
高度
?如果其中一个是字符串,那么它就不会被初始化了曾经满足[-4~4]范围条件。另外,更一般的建议是,避免太多类型转换。只需到处使用数字,存储向量对象(
{x:n,y:n'}
)。请提供一个。目前有太多未知项,我们无法帮助您。此
循环
对象是如何初始化的?它的
y
x
width
height
?如果其中一个是字符串,它将永远不会满足[-4~4]范围条件。另外,更一般的建议是,避免太多类型转换。只需到处使用数字,存储向量对象(
{x:n,y:n'}
)。