Hey guys, I have a problem here which I am trying to fix. I have a for loop, and inside it there are 3 cases that I want to implement randomly. The cases involve asteroids falling down a screen on different directions. What I want to do is to use a random variable so that somehow each case is selected randomly (for example, the random var turns into 1 and then the case associated with 1 occurs, and then start over again.
I tried using a Random Array List called randoms, but it doesn't seem to work. Could anyone tell me what is wrong here?
// draw asteroid for(int i = 0; i <= asteroids.size() - 1; i++) { if(!status.isNewAsteroid()) { int loop = randoms.get(i).nextInt(2); if(loop == 0) { asteroids.get(i).translate(0, asteroids.get(i).getSpeed()); // Displays one asteroid on the screen: graphicsMan.drawAsteroid(asteroids.get(i), g2d, this); } else if(loop == 1) { asteroids.get(i).translate(asteroids.get(i).getSpeed(), asteroids.get(i).getSpeed()); // Displays one asteroid on the screen: graphicsMan.drawAsteroid(asteroids.get(i), g2d, this); } else if(loop == 2) { asteroids.get(i).translate(-asteroids.get(i).getSpeed(), asteroids.get(i).getSpeed()); // Displays one asteroid on the screen: graphicsMan.drawAsteroid(asteroids.get(i), g2d, this); } }
Is there an alternate way of doing this? Or maybe could someone give me a tip? Thanks you all very much.