微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我没有在行中输入 0 时,为什么它说 0 未定义?

如何解决当我没有在行中输入 0 时,为什么它说 0 未定义?

我正在 p5.js 中开发一个程序。我遇到了错误消息的问题,该消息不断弹出。该程序长达 200 多行。
这是链接(使用 web p5 编辑器创建)。 https://editor.p5js.org/Meowmeow/sketches/b4AhGA4xH

代码示例:

var cols,rows;
var diffeculty = 0;
var w = 50;
var grid = [];
var stack = [];
var current;
var check = []
var k = 0;
var m = 0;
var won = false;

function setup() {
  //frameRate(10)
  createCanvas(600,400);
  cols = floor(width / w)
  rows = floor(height / w)


  for (var j = 0; j < rows; j++) {
    for (var i = 0; i < cols; i++) {
      var cell = new Cell(i,j)
      grid.push(cell)
    }
  }

  current = grid[0];
}
function index(i,j) {

  if (i < 0 || j < 0 || i > cols - 1 || j > cols - 1) {
    return -1;
  }
  return i + j * cols
}

function Cell(i,j) {


  this.visited = false;
  this.i = i;
  this.j = j;
  this.walls = [true,true,true];
  this.checkNeighbors = function() {
    var neighbors = []
    var top = grid[index(i,j - 1)]
    var right = grid[index(i + 1,j)]
    var left = grid[index(i - 1,j)]
    var bottom = grid[index(i,j + 1)]
    var Wall = this.walls;
    
    var wall=[]
    wall.push(i,j,Wall)
    
    
    if (top && !top.visited) {
      neighbors.push(top);
    }

    if (right && !right.visited) {
      neighbors.push(right);
    }
    if (bottom && !bottom.visited) {
      neighbors.push(bottom);
    }
    if (left && !left.visited) {
      neighbors.push(left);
    }

    if (neighbors.length > 0) {
      var r = floor(random(0,neighbors.length))
      return neighbors[r];
    } else {
      return undefined;
    }

  }

  this.highlight = function() {
    var x = this.i * w;
    var y = this.j * w;
    nostroke();
    fill(0,255,100)
    rect(x,y,w,w);

  }
  this.show = function() {
    var x = this.i * w;
    var y = this.j * w;
    stroke(255);
    noFill();

    if (this.walls[0]) {
      line(x,x + w,y);
    }
    if (this.walls[1]) {
      line(x + w,y + w);
    }

    if (this.walls[2]) {
      line(x + w,y + w,x,y + w);
    }
    if (this.walls[3]) {
      line(x,y);
    }

    if (this.visited) {
      fill(255,100);
      nostroke();
      rect(x,w);
    }
  }
};






function removeWalls(a,b) {
  var x = a.i - b.i;
  if (x === 1) {
    a.walls[3] = false;
    b.walls[1] = false;
  } else if (x === -1) {
    a.walls[1] = false;
    b.walls[3] = false;
  }
  var y = a.j - b.j;

  if (y === 1) {
    a.walls[0] = false;
    b.walls[2] = false;
  } else if (y === -1) {
    a.walls[2] = false;
    b.walls[0] = false;
  }


}

function draw() {
  background(51);
  for (var i = 0; i < grid.length; i++) {
    grid[i].show()
  }


  //Step One
  current.visited = true;
  current.highlight();

  var next = current.checkNeighbors()
  if (next) {
    next.visited = true;

    //Step 2
    stack.push(current);
    check.push(current)

    //Step 3
    removeWalls(current,next) 
    {}


    //Step 4 
    current = next;
  } else if (stack.length > 0) {
    current = stack.pop();

  }


  if (stack[0] === undefined) {
    fill(0,105,100)
    rect(0,w)
    rect(width - w,height - w,w)
    frameRate(8)
  
    
    var c = check.find(k/w,m/w);
    
    
    console.log(c)
     
    if(keyIsDown(UP_ARROW)) {
      k -= w
    }
    if (keyIsDown(RIGHT_ARROW)) {
      m += w
    }
    if (keyIsDown(DOWN_ARROW)) {
      k += w
    }
    if (keyIsDown(LEFT_ARROW)) {
      m -= w
    }

    fill(0,100)
    rect(m,k,w)

    if (m < 0) {
      m = 0
    }
    if (k < 0) {
      k = 0
    }
    if (m > width - w) {
      m = width - w
    }
    if (k > height - w) {
      k = height - w
    }

    if (k === height - w && m === width - w || won === true) {

      won = true
      background(0)
      textSize(40)
      text("You Win",width / 2,height / 2)
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.min.js"></script>

它总是显示 Uncaught TypeError: 0 is not a function (sketch: line 180),即使在第 180 行,我没有输入 0。

  • 程序应该创建一个迷宫,然后玩家必须解决迷宫。
  • 我使用 Coding Train toturial 制作了迷宫生成器。
  • 现在我正在尝试从数组中获取值,如果数组显示墙在那里,那么它就不允许它向那个方向移动。

如果可能,请写下是否有一种非常简单的方法可以让墙壁起作用?

解决方法

显然您正在尝试查找 i == k/wj == m/w 所在的单元格:

var c = check.find(k/w,m/w);

var c = check.find(cell => cell.i == ~~(k/w) && cell.j == ~~(m/w));

参见 Array.prototype.find()Integer division in JavaScript?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。