简体   繁体   English

JavaScript p5.js:ReferenceError

[英]JavaScript p5.js: ReferenceError

My code so far is supposed to be able to create a blank canvas with a snake and food, everytime my snake is on the food, the food should find a new location and the food is supposed to be aligned with the snake on a 'grid' but right now it is not. 到目前为止,我的代码应该能够用蛇和食物创建一个空白画布,每次我的蛇在食物上时,食物都应该找到一个新的位置,并且食物应该与网格上的蛇对齐。 '但现在不是。 That is my first problem and also in the console I do get this error: 那是我的第一个问题,在控制台中我也得到了这个错误:

ReferenceError: floor is not defined

Note that floor is supposed to be defined by p5.js . 请注意, floor应该被定义p5.j​​s

How to do this and how to fix the error? 怎么做以及如何纠正错误?

Here is my code: 这是我的代码:

var snake;

var scl = 10;
var food;

var columns = floor(width/scl);
var rows = floor(height/scl);

function setup()
{
  //Sets the Canvas
  createCanvas(700, 700);

  //Creates a new object using the variable snake
  snake = new Snake();

  food = new Food();

  //Sets the frame rate
  frameRate(10);

}

function draw()
{
  //Sets the Background, number implies the colour
  background(45);

  //Adds all the values set within the function to the snake
  snake.updateSnake();
  snake.showSnake();
  snake.keyPressed();

  food.showFood();
  food.updateFood();

  if(snake.eatFood(food))
  {
    food.updateFood();
  }
}


function Food()
{
  this.x = random(0,700);
  this.y = random(0,700);

  this.updateFood = function()
  {
    this.pos = createVector(floor(random(columns)), floor(random(rows)));
    this.pos.mult(scl);
  }

  this.showFood = function()
  {
    fill(255, 0, 10);
    rect(food.x, food.y, scl, scl);
  }

}

function Snake()
{
  this.x = 0;
  this.y = 0;

  this.xspeed = 0;
  this.yspeed = 0;

  this.updateSnake = function()
  {
    this.x = this.x + this.xspeed * scl;
    this.y = this.y + this.yspeed * scl;

    this.x = constrain(this.x, 0, width - scl);
    this.y = constrain(this.y, 0, height - scl);
  }

  this.showSnake = function()
  {
    fill(255);
    rect(this.x, this.y, scl, scl);
  }

  this.direction = function(x, y)
  {
    this.xspeed = x;
    this.yspeed = y;
  }

  this.eatFood = function(pos)
  {
    var distance = dist(this.x, this.y, pos.x, pos.y);

    if(distance < 1)
    {
      return true;

      console.log("WITHIN RANGE");

    }else
    {
      return false;

      console.log("OUTSIDE RANGE");

    }
  }

  this.keyPressed = function()
  {
    if (keyCode === UP_ARROW)
    {
      snake.direction(0, -1);
    } else if (keyCode === DOWN_ARROW)
    {
      snake.direction(0, 1);
    } else if (keyCode === RIGHT_ARROW)
    {
      snake.direction(1, 0);
    } else if (keyCode === LEFT_ARROW)
    {
      snake.direction(-1, 0);
    }
  }
}

The other answer is using the JavaScript Math.floor() function. 另一个答案是使用JavaScript Math.floor()函数。 You're trying to use the P5.js floor() function. 您正在尝试使用P5.js floor()函数。

You can't use P5.js functions until after the setup() function has been called. 在调用setup()函数之后,才能使用P5.js函数。 In other words, you want to do something like this: 换句话说,您想要执行以下操作:

var columns;
var rows;

function setup()
{
  //Sets the Canvas
  createCanvas(700, 700);

  //Creates a new object using the variable snake
  snake = new Snake();

  food = new Food();

  //Sets the frame rate
  frameRate(10);

  columns = floor(width/scl);
  rows = floor(height/scl);

}

More info can be found in the reference . 参考资料中可以找到更多信息。

(Disclaimer: the P5.js floor() function is almost definitely using the Math.floor() function behind the scenes. But in general you should use P5.js functions whenever you can. That's why they're there.) (免责声明:P5.js floor()函数几乎绝对是在后台使用Math.floor()函数。但是通常,您应该尽可能使用P5.js函数。这就是它们在那里的原因。)

Also note that you need to do this in the setup() function for similar reasons related to the width and height variables: they just have default values until after createCanvas() is called. 还要注意,出于与widthheight变量相关的类似原因,您需要在setup()函数中执行此操作:它们只有默认值,直到调用createCanvas()之后。

See also: Why can't I assign variables using p5 functions and variables before setup() ? 另请参见: 为什么在setup()之前不能使用p5函数和变量分配变量?

If you have follow-up questions, please post them in their own questions posts. 如果您有后续问题,请在他们自己的问题中发帖。 Please also try to narrow your problem down to a MCVE instead of posting your entire sketch. 也请尝试将问题缩小到MCVE,而不是发布整个草图。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM