0

我正在尝试访问Array<u32>AssemblyScript 中的元素。

我初始化并填充我的数组如下:

let totalSize: u32 = 512;

let leaderPtsX: u32[];
let leaderPtsY: u32[];
let leaderPtsType: u32[];

export function createPoints(): void {
  leaderPtsX = new Array<u32>();
  leaderPtsY = new Array<u32>();
  leaderPtsType = new Array<u32>();
  for (let i: u32 = 0; i < VP_COUNT; i++) {
    let x = <u32>(Math.random() * totalSize);
    let y = <u32>(Math.random() * totalSize);
    leaderPtsType.push(Math.random() > 0.5 ? 0xffffffff : 0xff00ff00);
    leaderPtsX.push(x);
    leaderPtsY.push(y);
  }
}

我有一个需要访问上述数组的函数:

function getClosestNeighbourType(x: u32, y: u32): u32 {
  let minDistance = 5000;
  let minDistanceType = -1;
  for (let i = 0; i < VP_COUNT; i++) {
    let dist =
      Math.abs(x - unchecked(leaderPtsX[i])) +
      Math.abs(y - unchecked(leaderPtsY[i]));
    if (dist < minDistance) {
      minDistance = <u32>dist;
      minDistanceType = unchecked(leaderPtsType[i]);
    }
  }
  return <u32>minDistanceType;
}

但是在浏览器中编译和运行它时,我得到以下控制台错误:

array.ts:104 Uncaught RuntimeError: memory access out of bounds
    at ~lib/array/Array<u32>#__unchecked_get (wasm-function[29]:0x1b4a)
    at assembly/index/getClosestNeighbourType (wasm-function[30]:0x1b84)

我尝试了很多不同的方法并阅读了很多文档,但没有骰子。任何帮助或链接表示赞赏。

谢谢!

4

0 回答 0