LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: phyzix5761 on November 18, 2012, 04:29:14

Title: For loop not working properly
Post by: phyzix5761 on November 18, 2012, 04:29:14
I have a for loop that is supposed to iterate between boxes I've created but once the keyboard event takes place it only iterates through the first box created. How can I get it to iterate through each box? Here is the code that is giving me problems:

while(!Display.isCloseRequested())
        {
            glClear(GL_COLOR_BUFFER_BIT);
           
            while(Keyboard.next())
            {
                if(Keyboard.getEventKey() == Keyboard.KEY_C &&
                        Keyboard.getEventKeyState())
                {
                    shapes.add(new Box(Mouse.getX()-25, 480 - Mouse.getY()-25));
                }
            }
           
            if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
            {
                Display.destroy();
                System.exit(0);
            }
           
            for(Box box : shapes)
            {
                if(Mouse.isButtonDown(0) && !somethingIsSelected)
                {
                   
                    somethingIsSelected = true;
                   
                        if(box.inBounds(Mouse.getX(), 480 - Mouse.getY()))
                        {
                            box.selected = true;
                        }
                   
                   
                }
               
                if(!Mouse.isButtonDown(0) && somethingIsSelected)
                {
                    System.out.println(box);
                    box.selected = false;
                    somethingIsSelected = false;
                }
               
                if(box.selected)
                {
                    box.update(Mouse.getDX(), Mouse.getDY());
                }
               
                   
                box.draw();
            }
         
           
            //int mousey = 480 - Mouse.getY()-1;
            //int xvelocity = Mouse.getDX();
            //int yvelocity = -Mouse.getDY();// must make oposite
           
           // System.out.println(xvelocity+" "+yvelocity);
                       
            Display.update();//update window
            Display.sync(60);//sync to 60 fps
        }
Title: Re: For loop not working properly
Post by: Fool Running on November 19, 2012, 18:16:41
What exactly is your problem? You say that it only iterates the first box, but there is no break statement in your loop so there is no way for the loop not to iterate over all the boxes.

I can see that there is a problem with selecting a box/selecting a different box. If selecting a box is your problem, then I think you need code more like:

           for(Box box : shapes)
           {
               if(Mouse.isButtonDown(0))
               {
                       box.selected = box.inBounds(Mouse.getX(), 480 - Mouse.getY());
               }
               
               if(box.selected)
               {
                   box.update(Mouse.getDX(), Mouse.getDY());
               }
                   
               box.draw();
           }