We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a question about some code I'm working on. I am trying to make a game and I want to increase the speed of the balls after the timer has expanded. I set this timer for every 5 seconds, but only one ball's speed increases instead of all the balls. Also, it sometimes decreases in speed while I only want it to increase slowly every five seconds. Does someone have an idea on what I am doing wrong here?
` int Bal = 45; Timer timer; int ball1[] = new int[Bal]; int ball2[] = new int[Bal]; float speed1[] = new float[Bal]; float speed2[] = new float[Bal]; color colorBalls = color(45, 75, 255);
void setup(){
size(800, 800);
for(int i = 0; i < Bal; i++){ // initializing all the variables for the balls
ball1[i] = int(random(50, width+60));
ball2[i] = int(random(50, width-75));
speed1[i] = 1;
speed2[i] = 1;
timer = new Timer(5000);
timer.start();
}
}
void draw(){
background(200,0,0);
rectMode(CENTER);
rect( mouseX,mouseY,25,25);
noCursor();
for(int i = 0; i<Bal; i++){
fill(colorBalls);
ellipse(ball1[i], ball2[i], 20, 20);
if(ball1[i] > width-10 || ball1[i] < 10){
speed1[i] = speed1[i] * (-1);
}
if(ball2[i] > height-10 || ball2[i] < 10){
speed2[i] = speed2[i] * (-1);
}
ball1[i] += speed1[i];
ball2[i] += speed2[i];
}
if(timer.done()){ int i= 2;
speed1[i] = speed1[i]+ 3;
speed2[i] = speed2[i] + 3;
timer.start();
}
}
class Timer {
int storedTime;
int totalTime;
Timer(int allTotalTime) {
totalTime = allTotalTime;
}
void start() {
storedTime= millis();}
boolean done() {
int passedTime = millis()- storedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}`
Answers
edit post, highlight code, press ctrl-o to format.