For loop not working properly

Started by phyzix5761, November 18, 2012, 04:29:14

Previous topic - Next topic

phyzix5761

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
        }

Fool Running

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();
            }
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D