Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.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 Dofus最近入口_Javascript - Fatal编程技术网

Javascript Dofus最近入口

Javascript Dofus最近入口,javascript,Javascript,嗨,我来问你如何用JavaScript计算坐标。 我是Dofus游戏的玩家,在Dofus中有坐标 右侧>+1, 左>-1, Top>-1, 底部>+1 这是游戏中的坐标。 在Dofus中有Zaap(世界上的固定传送门户,其位置不变,例如:[-20;-20])。我想根据用户给出的坐标,例如[-6;11],知道哪个Zaap离这个位置最近 如果它可以是样式的函数: 函数getNearestPortal(myposition){} 那太酷了 互动地图: 入口位置: [-5,8] [3,-5] [5,7]

嗨,我来问你如何用JavaScript计算坐标。 我是Dofus游戏的玩家,在Dofus中有坐标

右侧>+1
左>-1
Top>-1
底部>+1

这是游戏中的坐标。 在Dofus中有Zaap(世界上的固定传送门户,其位置不变,例如:
[-20;-20]
)。我想根据用户给出的坐标,例如
[-6;11]
,知道哪个Zaap离这个位置最近

如果它可以是样式的函数:
函数getNearestPortal(myposition){}
那太酷了

互动地图:

入口位置:

[-5,8]
[3,-5]
[5,7]
[-2,0]
[-1,24]
[7,4]
[4,-19]
[10,22]
[13,26]
[13,35]
[-32,-56]
[-26,35]
[35,12]
[27,-14]
[25,-4]
[-24,12]
[-16,1]
[17,-31]
[23,-22]
[29,49]
[26,-37]
[30,-38]
[-13,-28]
[-5,-23]
[-14,-47]
[-20,-20]
[1,-32]
[-34,-8]
[-18,-26]
[-53,18]
[-46,18]
[-54,16]
[-67,-75]
[-78,-41]
[-77,-73]
如果你需要更多的信息,问我

这是一个复杂的要求,比许多人不玩的游戏更复杂:/


提前感谢。

以下是我根据您的需要改编的功能:

注意:这假设您的坐标存储在嵌套数组中,如果这不适合您的需要,请随时告诉我


//Store all coordinates in a 2D array
let coords = [[-5,8],[3,-5],[5,7],[-2,0],[-1,24],[7,4],[4,-19],[10,22],[13,26],[13,35],[-32,-56],[-26,35],[35,12],[27,-14],[25,-4],[-24,12],[-16,1],[17,-31],[23,-22],[29,49],[26,-37],[30,-38],[-13,-28],[-5,-23],[-14,-47],[-20,-20],[1,-32],[-34,-8],[-18,-26],[-53,18],[-46,18],[-54,16],[-67,-75],[-78,-41],[-77,-73]];

//myposition is an array of length 2 in the form of [x, y]
function getNearestPortal(myposition){

  closest = [null, null];
  let distance = Infinity;

  //Loop through coords and check if the current position is closest to the given point
  for(const [x, y] of coords){
    let d = Math.sqrt((myposition[0] - x) ** 2 + (myposition[1] - y) ** 2);
    if(d < distance){
      closest = [x, y];
      distance = d;
    }
  }

  return closest;
};

getNearestPortal([15, 20]);
//[10, 22]

//将所有坐标存储在二维数组中
让coords=[-5,8],[3,-5],[5,7],-2,0],-1,24],[7,4],[4,-19],[10,22],[13,26],[13,35],-32,-56],-26,35],[35,12],[27,-14],[25,-4],-24,12],[17,-31],[23,-22],[29,49],[26,-37],[30,-38],-28],-5,--23],-14,--47],-20],[1,--32-26],-18],-18],[17,--67],-77];
//myposition是长度为2的数组,形式为[x,y]
函数getNearestPortal(myposition){
最近的=[null,null];
让距离=无穷远;
//通过坐标循环,检查当前位置是否最接近给定点
for(坐标的常数[x,y]{
设d=Math.sqrt((myposition[0]-x)**2+(myposition[1]-y)**2);
如果(d<距离){
最近的=[x,y];
距离=d;
}
}
返回最近的位置;
};
getNearestPortal([15,20]);
//[10, 22]

它工作得很好,非常感谢,我已经找了好几个小时了……对不起,是的,但是你必须解释游戏,这样你才能理解,而且我不是英语,所以有两个困难,你知道吗?但我同意这一点。