Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  -= operator making values negative when they shouldnt be?? O_o  (Read 520 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345
« Posted 2012-12-10 00:58:28 »

For some reason in my android game I am making I can add 2 to a value by doing
1  
variableName += 2;

but when I do
1  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
      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;
      }
Offline Jimmt
« Reply #1 - Posted 2012-12-10 01:00:21 »

That's probably because
1  
World.score - 10 >= currentScore
is true more often than
1  
2  
(World.collide(Assets.snowballFast, Assets.sprite1, fastballzX,
            fastballzY, GameScreen.mainX, GameScreen.mainY)
, right?
Offline wreed12345
« Reply #2 - Posted 2012-12-10 01:01:34 »

no sorry thats not right Sad
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #3 - Posted 2012-12-10 01:21:26 »

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.

Offline Best Username Ever

Junior Member





« Reply #4 - Posted 2012-12-10 02:33:16 »

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

JGO Knight


Medals: 28
Projects: 1


Not a glitch. Just have a lil' pixelexia...


« Reply #5 - Posted 2012-12-10 02:49:47 »

Definitely it is happening within
World.collide(Assets.snowballFast, Assets.sprite1, fastballzX,
            fastballzY, GameScreen.mainX, GameScreen.mainY)
.
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.
Offline wreed12345
« Reply #6 - Posted 2012-12-10 03:21:01 »

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?
Offline Jimmt
« Reply #7 - Posted 2012-12-10 03:39:35 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
HeroesGraveDev (42 views)
2013-06-15 23:35:23

Vermeer (51 views)
2013-06-14 20:08:06

davedes (48 views)
2013-06-14 16:03:55

alaslipknot (44 views)
2013-06-13 07:56:31

Roquen (62 views)
2013-06-12 04:12:32

alaslipknot (50 views)
2013-06-10 19:30:18

HeroesGraveDev (67 views)
2013-06-09 04:36:03

alaslipknot (54 views)
2013-06-09 03:40:19

CodeHead (54 views)
2013-06-09 02:55:41

GabrielBailey74 (66 views)
2013-06-09 00:02:25
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!