problem to create an object list.

I want to create an object list in another object and have access later to the information contained in the objects of this list. The problem is that when I create a list I can not access this list later. I will send you the address of my project if someone can help me. https://gitlab.com/codemasterjr/Schematic_Clock.git I want to create a list of object "pin" in the object "dip" which does not always contain the same number of pin. thank you in advance

Answers

  • Please don't link to your entire project. Please post a MCVE that shows the problem in a small example sketch.

  • edited January 11

    I want to create a list or array of object "pin" in the object "dip" the number of pin will be not the same all the time. And I want to have acces to this "pin" position. Now I'm just able to draw all the "pin" on the canvas but I dont have acces to his position

    var dip1;
    
    function setup() {
      createCanvas(1920, 1080);
      background(200);
      colorOn = color(255, 0, 0);
      colorOff = color(25, 0, 0);
      colorOver = color(0, 255, 0);
      dip1 = new Dip(8, 50, 100, "TEST");
    }
    
    function draw() {
      dip1.show();
    }
    
    var offset = 20;
    class Dip {
      constructor(numberOfPin, posX, posY, name) {
        var pinCount;
        var dipHeight;
        var dipWidth;
        var name;
        this.name = name;
        this.x = posX;
        this.y = posY;
        this.pinCount = numberOfPin;
        this.dipWidth = 80;
        this.dipHeight = ((this.pinCount / 2) * (pinDim * 2)) + (offset * 2) - pinDim;
      }
      show() {
        rectMode(CORNER);
        noStroke();
        fill(50);
        rect(this.x, this.y, this.dipWidth, this.dipHeight);
        fill(80);
        arc(this.x + (this.dipWidth / 2), this.y, this.dipWidth / 2, this.dipWidth / 2, 0, PI);
    //These two loop juste draw the pin
    //I want to have acces to all the pin object
        for (var i = 0; i < this.pinCount / 2; i++) {
          var pin = new Pin(i, pin_in, this.x - (pinDim / 2), this.y + offset + (pinDim / 2) + (i * pinDim * 2));
          pin.show();
        }
        for (var i = 0; i < this.pinCount / 2; i++) {
          var pin = new Pin(i, pin_in, this.x + this.dipWidth + (pinDim / 2), this.y + offset +   (pinDim / 2) + (i * pinDim * 2));
          pin.show();
        }
      }
    }
    //-----------------------------pin.js
    //I want to have acces to the position of each pin I have created.
    var pinDim = 10;
    class Pin {
      constructor(tempPinNumber, pinType, posX, posY) {
        var pinNumber
        var state;
        this.state = 0;
        this.x = posX;
        this.y = posY;
        pinNumber = tempPinNumber;
      }
      show() {
        noStroke();
        fill(100);
        rectMode(CENTER);
        rect(this.x, this.y, pinDim, pinDim);
      }
    
  • The M in MCVE stands for minimal. You need to isolate the problem. Also please fix the formatting of your code using the code button (it looks like a C).

  • edited January 11

    I have already try this

    class Dip {
      constructor(numberOfPin, posX, posY, name) {
        var pinCount;
        var listPin = []
        var dipHeight;
        var listPin;
        var dipWidth;
        var name;
        this.name = name;
        this.x = posX;
        this.y = posY;
        this.pinCount = numberOfPin;
        this.dipWidth = 80;
        this.dipHeight = ((this.pinCount / 2) * (pinDim * 2)) + (offset * 2) - pinDim;
    
        for(var i = 0;i<pinCount;i++){
            listPin[i] = new Pin(i, this.x, this.y);
        }
    
      }
    

    But went I call my object Dip in the console I dont saw the list and his Pin object.

  • edited January 11 Answer ✓
    • You're confusing properties w/ variables inside your constructor()! :-SS
    • Although they're similar, they have some diffs., like scope and lifespan. L-)
    • For example, you're even declaring variable listPin[] not once but twice there! @-)
    • If you expect that to exist beyond Dip's constructor(), you should annex listPin[] as a property of the object being created instead. :-@
    • That is, the this object; which represents Dip's current newly instance: :-bd
      this.listPin = Array(numberOfPin);
  • Answer ✓

    Check this: http://p5js.sketchpad.cc/sp/pad/view/VguNAYr2CN/latest

    Kf

    "use strict";
    
    let dip1;
    let colorOn;
    let colorOff;
    let colorOver;
    
    
    function setup() {
      createCanvas(480, 640); 
    
      colorOn = color(255, 0, 0);
      colorOff = color(25, 0, 0);
      colorOver = color(0, 255, 0);
    
      dip1 = new Dip(8, 50, 100, "TEST");
    }
    
    function draw() {
      background(200);
      dip1.show();
    }
    
    class Dip {   
      constructor(numberOfPin, posX, posY, name) {
        this.pinDim=10;
        this.offset=10;
        this.pinCount = numberOfPin;
        this.pinList=[]  //new Array(this.pinCount);
        this.name = name;
        this.x = posX;
        this.y = posY;    
        this.dipWidth = 80;
        this.dipHeight = ((this.pinCount / 2) * (this.pinDim * 2)) + 
                                  (this.offset * 2) - this.pinDim;
    
        for (let i = 0; i < this.pinCount / 2; i++) {
          let xc=new Pin(i, 1001, 
          this.x - (this.pinDim / 2), 
          this.y + this.offset + (this.pinDim / 2) + (i * this.pinDim * 2),
          this.pinDim);
    
          this.pinList.push(xc);
        }
      }
    
      //Method
      show() {    
        rectMode(CORNER);
        noStroke();
        fill(50);
        rect(this.x, this.y, this.dipWidth, this.dipHeight);
        fill(80);
        arc(this.x + (this.dipWidth / 2), this.y, this.dipWidth / 2, this.dipWidth / 2, 0, PI);
    
        for (let i = 0; i < this.pinCount / 2; i++) {
          this.pinList[i].show();
        }
      }
    }
    
    class Pin {
      constructor(tempPinNumber, pinType, posX, posY,pinDim) {
        this.state = 0;
        this.x = posX;
        this.y = posY;
        this.pinNumber = tempPinNumber;
        this.pType=pinType;
        this.pinDim = pinDim;
      }
      show() {
        noStroke();
        fill(100);
        rectMode(CENTER);
        rect(this.x, this.y, this.pinDim, this.pinDim);
      }
    }
    
  • thank you very much. It works ;-)

Sign In or Register to comment.