首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript: p5.js未知错误

JavaScript: p5.js未知错误
EN

Stack Overflow用户
提问于 2017-07-01 02:41:23
回答 3查看 367关注 0票数 0

我只是写出我的代码,我保存它来运行它,它不再工作了,我找不到问题,因为我在这方面相当新手,我正在使用p5.js,这是我的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2017-07-01 02:53:21

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

运行以下代码:

代码语言:javascript
复制
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;
  }
}
代码语言:javascript
复制
<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>

票数 5
EN

Stack Overflow用户

发布于 2017-10-08 22:25:08

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

票数 1
EN

Stack Overflow用户

发布于 2019-05-15 09:58:39

我相信你必须用this.direction = function(x, y)修改这条指令this.direction(x, y)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44852980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档