Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
P5.js change stroke of object instance stored in array affects wrong instance

If i click on the first ball, the second shows up as selcted. if i click on the second one the third one shows up as selected and so on.

But the boolean value is set correctly.

I can not find the problem in the code, it should be some logic error.

I appreciate any help, thank you.

have fun with this paradox ;)

my sketch.js

let balls = [];
let ballAmount = 3;


function setup() {
  
  let red = color(255, 0, 0);
  let green = color(0,255,0);
  let blue = color(0,0,255);

  let colors = [red, green, blue];
  
  
  createCanvas(innerWidth, innerHeight);
  noStroke();
  

  for (let i = 0; i < ballAmount; i++) {
    let tempX = 200 + (100 * i);
    balls.push(new Ball(tempX, 200, 20, colors[i])); 
  }
}

function draw() {
  background(0);
  
  balls.forEach((ball) => {
    ball.show();
  });
}

function mousePressed() {
  
  print(balls);
  balls.forEach((ball) => {
    ball.clicked();
  });
}

my ball.js (class)

class Ball {
  constructor(x, y, r, color){
    this.pos = new p5.Vector();
    this.pos.x = x;
    this.pos.y = y;
    this.r = r;
    this.isSelected = false; 
    this.ballFill = color;
  }
  
  show() {
    fill(this.ballFill);
    ellipse(this.pos.x, this.pos.y, this.r * 2);
    
    if (this.isSelected){
      stroke(255, 255, 0);
      strokeWeight(4);
    } else {
      noStroke();
    }
  }
  
  clicked() {  
    let d = dist(mouseX, mouseY, this.pos.x, this.pos.y);
    if (d <= this.r) {
      this.isSelected = true;
    } else {
      this.isSelected = false;
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Three notes...

  1. Please be more elaborate in your question. Luckily your code kind of speaks for itself, but otherwise I would have had no idea what you were actually asking. Your post description was 'reasonable', but the title is kind of abhorrent.

  2. You seem to be drawing your ellipses with the idea that r is half the radius, thus you multiply the r by 2. You should use ellipseMode(RADIUS) instead so it's more clear that that's the behavior you want. That lets you input the radius as half the width of the circle, like you're using your r. See this page for more information.

  3. The reason you always outline the 'next' ball instead of the one you clicked is because you draw the ball BEFORE changing the stroke values:

this

show() {
  fill(this.ballFill);
  ellipse(this.pos.x, this.pos.y, this.r);

  if (this.isSelected){
    stroke(255, 255, 0);
    strokeWeight(4);
  } else {
    noStroke();
  }
}

should be

show() {
  fill(this.ballFill);
  if (this.isSelected){
    stroke(255, 255, 0);
    strokeWeight(4);
  } else {
    noStroke();
  }
  ellipse(this.pos.x, this.pos.y, this.r);
}

You were drawing the selected ball with stroke disabled, then you enabled the stroke, then you drew your next, unselected ball WITH the stroke you just enabled, after which you then disabled the stroke again.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda