你的算法有一个错误,但它确实有效。
我不完全理解你的代码,但如果我不得不猜测, 条件
info.x == x
期望每次“跳跃”时 x
都会递增
找到(在本例中已过滤)。
所以你不能只将这个 jumpAmounts
偏移量存储在变量中
稍后将其添加到x
。
我完全删除了过滤功能,并将其替换为 for 循环,其中
x
“当场”递增:
// old code
const jumpAmounts = jumps.filter(info => info.y == y && info.x == x);
if (jumpAmounts && jumpAmounts.length > 0)
x += jumpAmounts.length;
新代码:
for (let info of jumps)
if (info.y === y && info.x === x)
x++;