p5.js creating something after a certain amount of time has elapsed

sketch.js:

var number_of_explosions = 10;
var explosionarray = [];
var mycollision = false;

function setup(){
    for (var i = 0; i < number_of_explosions; i++) {
        explosionarray[i] = new explosion();
        console.log(explosionarray.length)
    }
}

function draw(){
    createCanvas(400,400);
    background(155);
    for (var i = 0; i < explosionarray.length; i++) {
        mycollision = collision(explosionarray[i].position.x,explosionarray[i].position.y,25);
        if (mycollision){
            explosionarray[i].isdestroyed(true)
        }
        if (explosionarray[i].destroyed == false){
            explosionarray[i].display();
        }
    }
}

function collision(xpos,ypos,radius){
    var distance = dist(xpos,ypos,mouseX,mouseY);
    if (distance <= radius){
        return true;
    }
    else{
        return false;
    }
}

explosion.js:

function explosion(){
    this.w = 50;
    this.h = 50;
    this.destroyed = false;

    this.position = createVector(random(25,375), random(25,375));

    this.display = function(){
        fill(100);
        ellipse(this.position.x, this.position.y, this.w, this.h);  
    }

    this.isdestroyed = function(dbool){
        if (dbool){
            this.destroyed = true;
        }
        else{
            this.destroyed = false;
        }
    }
}

Hi all. Any help would be greatly appreciated.

This code basically draws circles on the screen and tracks the collision between the mouse and the circle. When the mouse has collided with the circle it is no longer drawn.

What I want to do though is not draw all the circles at once but with a delay between them. e.g. draw an 'explosion' track the collision and after a random amount of time has passed say between 1 to 3 seconds it draws the second one and then tracks both the first and second collisions.

I was thinking about using the time functions for p5 but cant get my head around when it switches from 59 back to 0!

Say on one cycle i have var take_time = second() this is then stored and compared to the random number. But if I scan at 58 and the random number is 3 I am looking for the take_time to now be 1! I know this must be possible but my little brain is struggling. Cheers!

Tagged:

Answers

Sign In or Register to comment.