1

I have looked into JS and the P5.js references and I can't figure out what's wrong with this code. I'm using p5.js with the DOM and Sound libraries

var year = 0 
var yearST

function setup() {
  createCanvas(1000, 750);

}

function draw() {

  background(210)
  textAlign(RIGHT,TOP)
  yearST = str(year)

  textSize(15)
  console.log(yearST) //This is the var that matters
  text(("Years survived: " + yearST), 990, 10)
}

The console.log() that logs yearST returns

function () { [native code] }

Please help, what's the problem with this code. (No changes when adding semicolons, I tried)

This is what it looks like when it is run in a browser: 1, That's supposed to be a 0 at the end of the string

1 Answers1

0

The problem is that year is already a function in the P5.js library. You can find it in the reference here.

So when you call str(year) you're really passing in the year() function, not your year variable.

Just rename your year variable to something that doesn't collide with an existing function, or use instance mode to prevent P5.js from cluttering the global namespace like this.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107