JavaScript:p5.js未知错误

时间:2017-06-30 18:41:23

标签: javascript p5.js

我只是写出我的代码,然后保存它来运行它,它只是不再工作了,我无法找到问题,因为我对此非常苛刻,我使用的是p5.js,这是我的代码:< / p>

var s;

function setup()
{
 createCanvas(700, 700);
 s = new Snake();
}

function draw()
{
  background(51);
  s.update();
  s.show();
}

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

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

  this.update = function()
  {
    this.x = this.x + this.xspeed;
    this.y = this.y + this.yspeed;
  }

  this.show = function()
  {
    fill(255);
    rect(this.x, this.y, 10, 10);
  }

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

这是HTML:

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>
    <script src="main().js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
  <style> body {padding: 0; margin: 0;} </style>
  </head>
  <body>
  </body>
</html>

3 个答案:

答案 0 :(得分:3)

this.direction(x, y)应为this.direction = function(x, y)

运行以下代码:

&#13;
&#13;
var s;

function setup()
{
 createCanvas(700, 700);
 s = new Snake();
}

function draw()
{
  background(51);
  s.update();
  s.show();
}

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

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

  this.update = function()
  {
    this.x = this.x + this.xspeed;
    this.y = this.y + this.yspeed;
  }

  this.show = function()
  {
    fill(255);
    rect(this.x, this.y, 10, 10);
  }

  this.direction = function(x, y)
  {
    this.xpeed = x;
    this.yspeed = y;
  }
}
&#13;
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>
    <script src="main().js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
  <style> body {padding: 0; margin: 0;} </style>
  </head>
  <body>
  </body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试将功能this.direction(x,y)更改为this.direction = function(x.y)

答案 2 :(得分:0)

我相信您必须将this.direction(x, y)的说明this.direction = function(x, y)更改为