-= operator making values negative when they shouldnt be?? O_o

For some reason in my android game I am making I can add 2 to a value by doing

variableName += 2;

but when I do

variableName -= 2;

the variableName becomes negative for some reason when the original value of the variable is 5, here is the code I am using


		if (World.collide(Assets.snowballFast, Assets.sprite1, fastballzX,
				fastballzY, GameScreen.mainX, GameScreen.mainY)) {
			fastballzY = random.nextInt(10000) * -1 - 1000;
			fastballzX = random.nextInt(400) + 200;
			World.powerUp = true;
			World.fastBalls = true;
			currentScore = World.score;
			World.ballSpeeds[0] += 2;
			World.ballSpeeds[1] += 2;
			World.ballSpeeds[2] += 2;
			World.ballSpeeds[3] += 2;
		}
		if (World.score - 10 >= currentScore) {
			World.powerUp = false;
			World.fastBalls = false;
			World.ballSpeeds[0]-=2;
			World.ballSpeeds[1]-=2;
			World.ballSpeeds[2]-=2;
			World.ballSpeeds[3]-=2;
		}

That’s probably because

World.score - 10 >= currentScore

is true more often than

(World.collide(Assets.snowballFast, Assets.sprite1, fastballzX,
            fastballzY, GameScreen.mainX, GameScreen.mainY)

, right?

no sorry thats not right :frowning:

Then the original value is not 5.

Java is never wrong.
(dramatic silence)
Never.

debug this or System.out.println the variable values before and after your assignment.
You’ll see that the original value was not 5, or the code is being executed more than one time, what makes it negative after a few iterations.

First, learn how to use a debugger. Second, use loops instead of copying and pasting lines like [icode]World.ballSpeeds[n]-=2;[/icode].

Definitely it is happening within [icode]World.collide(Assets.snowballFast, Assets.sprite1, fastballzX,
fastballzY, GameScreen.mainX, GameScreen.mainY)[/icode].
I hope you know that if you put that function in the “if” statement, it will perform the function every loop which will change your values if the check passes or not. Just to make sure, you should post what that does.

I know how to use for loops but im just wondering would that have any effect on game performance if i use those a lot?

No. What it will do is save you a lot of time from copying+pasting.