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

150
Visualizações
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 Respostas
Responde à pergunta

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 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