Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 807 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« 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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2012-12-10 01:01:34 »

no sorry thats not right Sad

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« 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 Wizard


Medals: 98
Projects: 1
Exp: 7 years


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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« 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.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (64 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!