Presisor
Presisor

Reputation: 7

JavaScript: p5.js Unknown Error

I was just writing out my code and I save it to run it and its just not working anymore and I cant find the problem as I am quite noobish at this, I am using p5.js and here is my code:

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;
  }
}

And here is the 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>

Upvotes: 0

Views: 523

Answers (3)

Juan Le&#243;n
Juan Le&#243;n

Reputation: 11

I believe you have to change this instruction this.direction(x, y) by this.direction = function(x, y)

Upvotes: 1

Cobain Ambrose
Cobain Ambrose

Reputation: 91

try changing the function this.direction(x,y) to this.direction = function(x.y)

Upvotes: 1

user94559
user94559

Reputation: 60143

this.direction(x, y) should be this.direction = function(x, y)

Running code below:

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;
  }
}
<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>

Upvotes: 5

Related Questions