Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Error in object generator via button in p5.js

I am doing a logic gate simulator where I already have entities like switch, light and functions like wiring and more. But I wanted to create entities like switch and light via the button you click on and the object or entity will be created for you, but it doesn't work for me. Someone couldn't help me with that. Thanks in advance. The code I attach via the link will take you to the entire code and simulator in the online editor from p5.js.

Full Code: https://editor.p5js.org/jakubmitrega1/sketches/Mg1BGpimz

Relevant Code:

let entities = [];

function createEntity() {
  if (mousePressed == "Light") {
    entities.push(new Switch(100, 100))
  }

  if (mousePressed == "Light") {
    entities.push(new Light(100, 200))
  }
}
<div class="menu">
  <button onclick="createEntity('switch')">Switch</button>
  <button onclick="createEntity('light')">Light</button>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is pretty obvious why this isn't working once you isolate the code in question:

let entities = [];

function createEntity() {
  // Obvious typo here: "Light" instead of "Switch"
  // However, bigger issue, why would mousePressed be equal to any kind of
  // string at all? Much less one that will help you determine which button
  // was pressed.
  if (mousePressed == "Light") {
    entities.push(new Switch(100, 100))
  }

  if (mousePressed == "Light") {
    entities.push(new Light(100, 200))
  }
}

Since you are passing the type of entity as an argument to the createEntity function, the logical thing to do would be to have it actually specify an argument and check that instead of mousePressed:

let entities = [];

function createEntity(type) {
  // Note: string comparison is case sensitive.
  if (type === "switch") {
    alert("Create a Switch");
  } else if (type === "light") {
    alert("Create a Light");
  }
}
<div class="menu">
  <button onclick="createEntity('switch')">Switch</button>
  <button onclick="createEntity('light')">Light</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda