We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was following a tutorial on p5.js and after realized a constructor function my code didn t work the object didn t appear on the screen. Here is my code:
var sommet = [];
function setup() {
createCanvas(400, 400);
for(var i = 0; i < 4; i++)
{
sommet[i] = new Sommet();
}
}
function draw() {
background(100);
for(var j = 0; j < sommet.lenght; j++)
{
sommet[j].display();
}
}
function Sommet() {
this.x = random(0, width);
this.y = random(0, height);
this.display = function()
{
strokeWeight(10);
stroke(255);
point(this.x, this.y);
}
}
Thank you for helping me btw.
Answers
Please format your code. Edit post, select code and hit ctrl+o. Ensure there is an empty line above and below your code.
Kf
length
I hit post by mistake. Then figured it was enough of a clue 8)
Much more compact version, w/o the "problematic" length: ;;)
for (let s of sommet) s.display();
And while we're at it, how about modernize the whole sketch to use ES6 JS? <:-P
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
The code below can be copied and pasted and run at the following link: B-)
https://OpenProcessing.org/sketch/create
Thank you so much