-2

My program keeps not responding when starting the program when generating the terrain.

this is the code. I Believe that it's the for loop.

Random Random = new Random();
        int numberHeight = Random.nextInt(5);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glColor3f(0.5f,0.5f,1.0f);
        int Bottom = Height - 100;
        for(int xOffset = 0; xOffset < Width; xOffset = xOffset + 100){
            for(int currentHeight = 0; currentHeight < numberHeight; currentHeight = currentHeight++){
                GL11.glBegin(GL11.GL_QUADS);
                {
                    GL11.glVertex2f(xOffset,Bottom - currentHeight * 100);
                    GL11.glVertex2f(xOffset + 100,Bottom - currentHeight * 100);
                    GL11.glVertex2f(xOffset + 100,Bottom - currentHeight * 100 - 100);
                    GL11.glVertex2f(xOffset,Bottom - currentHeight * 100 - 100);
                }
                GL11.glEnd();
                if(currentHeight >= numberHeight)break;
            }
        }
ZaneGlitch
  • 21
  • 2
  • 3
    What kind of exception do you get? And in which line? – honk Sep 06 '14 at 19:13
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Sep 06 '14 at 20:02
  • I believe that the problem must be the for loops that may have rendered cubes out of bounds. – ZaneGlitch Sep 07 '14 at 06:56

1 Answers1

0

The problem is that the for loop isn't written correctly:

I tried this code:

public static void main(String []args){
   for(int i = 0;i < 10;i = i++)
   {
       System.out.println(i);
   }
}

It loops infinitly and prints out 0 all the time.

Your loop is:

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight = currentHeight++)

and should be

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight++)

or:

for(int currentHeight = 0; currentHeight < numberHeight; currentHeight += 1)

How increment / decrement operators work as prefix or suffix:

Java: Prefix/postfix of increment/decrement operators?

Community
  • 1
  • 1
Geosearchef
  • 600
  • 1
  • 5
  • 22
  • I changed it to CurrentHeight++ and it worked but it ended up making blocks in the same size in every blocks until it maybe could be because of those two for loops that's been a part of that caused it to do that. but anyways thanks for the issue. – ZaneGlitch Sep 13 '14 at 16:10