in short form, i created a math.random that will choose a random number, then another variable later will use that number. After that random is used math.random will choose another number, and this would go on forever. I would think to use a for loop with the math.random making it look something like this:
for (int i = 0; i <100; i++) {
int mRandom = (int) (Math.random() * 21 + 15);
}
then later I use the number to change the speed and angle of the sprite i created (this entire code is used to create the sprite so I don't have a problem with the sprite being used)
for (int i = mIDontKnowTimer; i == mRandom; i++) {
here I have a timer that i already created, and when that timer is equal to the number generated by mrandom the speed and angle changes
int pSpeed = (int) (Math.random() * 3 + 3);
int pAngle = (int) (Math.random() * 359);
mXSpeed = pSpeed * Math.cos(pAngle);
mYSpeed = pSpeed * Math.sin(pAngle);
}
Im confused as to how to make the mRandom a loop, because it is saying the first line has an illegal start of type and that it can't find "i" in the code
I'd first put Math.Random into a function and call it in my main loop.
public static int randomNumberGenerator(int min, int max){
Random rand = new Random();
return rand.nextInt(max - min) +min;
}
Now call it within a for/while
loop.
As written , your mRandom
is only valid within the scope of the first for
loop, and will be completely unavailable to your second for
loop. (I say "as written" because I suspect your code doesn't actually look like this - it would fail to compile.)
Your second loop isn't particularly clear to me, but it looks like you're trying to change your sprite's direction and speed mRandom
times? (Edit: comments confirm this.)
You could just use a nested for
loop to accomplish this:
int mRandom;
int pSpeed, pAngle;
for (int i = 0; i <100; i++) { //100 times in a row,
mRandom = (int) (Math.random() * 21 + 15); //generate a random number
for (int j = 0; j < mRandom; j++) { //count up to that random number
//and repeatedly adjust your sprite's properties
pSpeed = (int) (Math.random() * 3 + 3);
pAngle = (int) (Math.random() * 359);
mXSpeed = pSpeed * Math.cos(pAngle);
mYSpeed = pSpeed * Math.sin(pAngle);
}
}
It's worth noting that, unless you're redrawing your sprite somewhere in there, the extra speed and angle changes will be meaningless - you're updating those properties multiple times but only redrawing once, so only the most recent change will stick.
I think you're trying to do this
for (int i = 0; i <100; i++) {
int mRandom = (int) (Math.random() * 21 + 15);
for (int i = mIDontKnowTimer; i == mRandom; i++) {
....
}
}
The issue you are seeing is that you were trying to access the mRandom variable outside of it's scope.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.