My code don't work can you help me ?

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

  • I hit post by mistake. Then figured it was enough of a clue 8)

  • edited January 2017

    Much more compact version, w/o the "problematic" length: ;;)
    for (let s of sommet) s.display();

  • edited January 2017

    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

    // forum.Processing.org/two/discussion/20363/
    // my-code-don-t-work-can-you-help-me#Item_6
    
    // 2017-Jan-19
    
    "use strict";
    
    const sommets = Array(4);
    
    function setup() {
      createCanvas(400, 400);
      noLoop();
      for (let i = 0; i < sommets.length; sommets[i++] = new Sommet);
    }
    
    function draw() {
      background(0o200);
      Sommet.setStyle();
      for (let s of sommets)  s.display();
    }
    
    function mousePressed() {
      for (let s of sommets)  s.init();
      redraw();
    }
    
    class Sommet {
      constructor() {
        this.init();
      }
    
      static setStyle() {
        strokeWeight(10).stroke('yellow');
      }
    
      init() {
        this.x = random(width), this.y = random(height);
      }
    
      display() {
        point(this.x, this.y);
      }
    }
    
  • Thank you so much

Sign In or Register to comment.