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

141
Vistas
subtraction in p5.js if statement only occurs once instead of repeating

I have a p5.js animation here that is meant to animate the artwork then reverse the animation, however while it works for my other artworks, the logic doesn't work here as the if statement doesn't get triggered due to the subtraction for my mask only occurs once. Right now only have of the animation works, you can view it here: https://editor.p5js.org/theruantan/sketches/8g583LF8j

The main issue begins at line 116 and it is at line 124 where the code is unable to reach this if statement.

//Start of reversal for masks
for (let i = beachSwitches.length - 1; i >= 0; i--) {
          if (beachSwitches[i] == 0) {
            if (beachMasks[i] > 0) {
              beachMasks[i] -= 20;
              animating = true;
              //console.log("beachMasks");
              console.log(beachMasks[i])
            }
            if (beachMasks[i] <= 300) {
              // Begin reversing the next bar
              beachSwitches[i - 1] = 0;
              
              // To see if the code is able to reach here. 
              console.log(beachSwitches[i - 1] + "This is triggered");
              
            }
          }
}

beachMarks[i] should be able to be <= 300 but it's stuck at 780.

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

0

I have recently started working with p5.js but just based on reading the code and understanding what you are trying to do what I can see is in the line you mentioned i.e 116 , if you log inside and outside you will understand why its only running once. Inside the 116 lines for loop, you are checking for beachSwitches[i]==0, which only happens once(at the last index of that array) because the beachSwitches gets modified above, also you haven't defined anything in the else part for this condition. Which is what is beachSwtiches[i] == 0 is NOT TRUE. In my screenshot below, you might get a rough idea. Hope this helps in some manner! Cheers! Screenshot of logs I took from line 116 to 124

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