You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
p5.js says: [sketch.js, line 50] rect() was expecting Number for the second parameter, received an empty variable instead. If not intentional, this is often a problem with scope.
(in the rect inside draw method of the Person class.)
@dsaw there is conceptual misunderstanding
In JavaScript, the constructor must be named constructor(), not Player().
Player(x, y) { ... } should be replaced with constructor(x, y) { ... }. @kammeows you are right..
let player;
let pressedKey = {};
function setup() {
createCanvas(600, 600);
player = new Player(10, 10);
}
function draw() {
background(220);
player.draw();
}
class Player {
constructor(x, y) { // Corrected constructor
this.x = x;
this.y = y;
this.speed = 4;
}
p5.js version
No response
What is your operating system?
Windows
Web browser and version
Google Chrome 133.0.6943.142
Actual Behavior
Running the code at https://editor.p5js.org/devsaw115/sketches/rcla7OXeq causes below error
(in the rect inside draw method of the Person class.)
Expected Behavior
The code should run without the warning and correctly since the class syntax is correct.
Here is the reference with the same code that works correctly
https://editor.p5js.org/BarneyCodes/sketches/r-3a903da
Steps to reproduce
Steps:
this.x
insideplayer.draw
is undefined causing rect warnings inside consoleSnippet:
The text was updated successfully, but these errors were encountered: