jeudi 20 octobre 2016

Random Ball Speed in P5.js

I have the following code:

var posX1 = 0, posY1 = 100;
var speedX1 = random(1,3), speedY1 = random(1,3);

var posX2 = 0, posY2 = 200;
var speedX2 = 2, speedY2 = 0;

function setup()
{
    createCanvas(640, 480);
}

function draw()
{
    // redraw the background so you blank the screen
    background(255);

    if(posX1 > width)
    {
      speedX1 = -speedX1;
    }

    // update the position based on the speed
    posX1 += speedX1;
    posY1 += speedY1;

    // draw a ball
    strokeWeight(20);
    point(posX1, posY1);

    //
    posX2 += speedX2;
    posY2 += speedY2;

    //draw a ball
    strokeWeight(20);
    point(posX2, posY2);


}

Its in P5. I basically want the two circles to race each other at random speeds between 1 and 3 but instead they dont even appear on the screen. Can anyone point out where I'm going wrong?




Aucun commentaire:

Enregistrer un commentaire