1

I have a ball sprite and I need it to have a 50/50 chance to bounce off a platform sprite. Here is the code:

 bally=bally+3;
    ballx=ballx+3;
    ballx += xspd;
    bally += yspd;
    if(bally > 580 && bally <590 && ballx < mouseX+300 && ballx > mouseX-150){
      yspd*=-3;
      
    }
     else if(bally > 580 && bally <590 && ballx < mouseX+150 && ballx > mouseX){
      yspd*=+3;
      
      
    }
    if(ballx > 900){
      xspd*=-3;
    }
    if(ballx < 0){
      xspd*=+3;
    }
    if(bally < 0 && bally > 700){
      state=LOSE;
    }

This code make the ball bounce off the walls but once it bounces off the platform at the top it only bounces to the right. I need it to be a 50/50 chance to bounce either left or right. Thanks

I tried to make an else if statement before but that didn't work, along with changing the xspd variable in said else if statement.

Reed
  • 21
  • 1

1 Answers1

0

You need a source of random:

var randNum = random(0, 100);
if (randNum < 50) {
   text("Heads", 200, 200);
   direction = 1;
} else {
   text("Tails", 200, 200);
   direction = -1;
}

Use this to chose your sign. That should let chance choose your direction.

candied_orange
  • 7,036
  • 2
  • 28
  • 62