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

148
Vistas
how can i rewrite this code to make an object fall from top of canvas and when hits bottom it resets at top

I am trying to implement a group of objects that will fall from the top of the canvas in random spots, and then when they hit the bottom of the canvas they respawn at the top of the canvas. Ideally, I want to change this code in the future to use sprites that will fall from the top of the canvas and explode when they hit the bottom of the canvas but then respawn again at the top.

I have the bellow code that works when the mouse is pressed but I would like to rewrite this code so the event happens automatically without the mouse needed to be pressed to make the objects fall.

see code below.

var x, y;
var particle = [];

function setup() {
  createCanvas(720, 400);
  // Starts in the middle
  x = width / 2;
  y = height;
}

function draw() {
  background(0);
  if (mouseIsPressed){
  stroke(50);
  fill(100);
  ellipse(x, y, 24, 24);
  }
  x = x + 0;
  // Moving up at a constant speed
  y = y + 2;
  
  // Reset to the bottom
  if (y >= height) {
    y = 0;
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use the mousePressed() to add new particles to the list. The particles are added at the current mouse position (mouseX, mouseY). Move and draw the particles in a loop:

var particles = [];

function setup() {
    createCanvas(720, 400);
}

function mousePressed() {
    particles.push([mouseX, mouseY]);
}

function draw() {
    background(0);
    stroke(50);
    for (let i=0; i < particles.length; i++) {
        particles[i][1] += 2;
        if (particles[i][1] > height) {
            particles[i][1] = 0;
        }
        fill(100);
        ellipse(particles[i][0], particles[i][1], 24, 24);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

Alternatively, you can spawn the particles with a time interval. Get the number of milliseconds (thousandths of a second) since starting the sketch with millis(). The y-coordinate of a new particle is 0 and the x-coordinate is random():

var particles = [];

function setup() {
    createCanvas(720, 400);
}

function mousePressed() {
    particles.push([mouseX, mouseY]);
}

function draw() {
    let max_no_of_particles = 40;
    let expected_no_of_praticles = min(max_no_of_particles, millis() / 100);
    if (particles.length < expected_no_of_praticles) {
        particles.push([random(12, width-12), 0]);
    }

    background(0);
    stroke(50);
    for (let i=0; i < particles.length; i++) {
        particles[i][1] += 2;
        if (particles[i][1] > height) {
            particles[i][1] = 0;
        }
        fill(100);
        ellipse(particles[i][0], particles[i][1], 24, 24);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

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