Results 1 to 6 of 6
  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default For loop not working properly

    I'm working with lwjgl. 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:

    Java Code:
    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
            }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: For loop not working properly

    Are you sure that all your boxes are present in your shapes collection? Are you sure the for loop isn't looping through them all but only applying changes the the first one? Put a System.out.println in that loop and see how many times it actually loops.

  3. #3
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: For loop not working properly

    I know it loops through all the boxes until it gets to this statement:

    Java Code:
    if(Mouse.isButtonDown(0) && !somethingIsSelected) 
                    {
                         
                        somethingIsSelected = true;
                         
                            if(box.inBounds(Mouse.getX(), 480 - Mouse.getY()))
                            {
                                box.selected = true;
                            }
                        
                         
                    }
    There it stops looping for some reason. But outside that statement it is still looping. It's weird.

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: For loop not working properly

    outside that statement it is still looping.
    I'm not sure what that means.

    If you mean that the body of the if statement doesn't get executed then that's because the condition "Mouse.isButtonDown(0) && !somethingIsSelected" is false.

    As well as using System.out.println() to see how many times you go around the loop you can also use it to print the things that determine the behaviour inside the loop: box, Mouse.isButtonDown(0), somethingIsSelected, and box.selected. This is best done at the start of the loop.

  5. #5
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: For loop not working properly

    Let me show you what I mean. (Check comments in code).

    Java Code:
    for(Box box : shapes) 
                {
                    System.out.println(box);//this shows that it loops through every box
                    if(Mouse.isButtonDown(0) && !somethingIsSelected) 
                    {
                        System.out.println(box);//this shows that it only loops through the 1st
                        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();
                }

  6. #6
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: For loop not working properly

    Have a look at the state each time around the loop:

    Java Code:
    for(Box box : shapes) 
    {
        System.out.println("Entered loop with box=" + box);
        System.out.println("Mouse down=" + Mouse.isButtonDown(0));
        System.out.println("somethingIsSelected=" + somethingIsSelected + " box.selected=" + box.selected);
        if(Mouse.isButtonDown(0) && !somethingIsSelected) 
        {
            // etc
    Remember that nothing in the first if statement (eg the second printin() you have) will execute if Mouse.isButtonDown(0) is false or somethingIsSelected is false. Based on what you've posted both of these are capable of changing from one loop iteration to the next - hence the need to print them out each time.

Similar Threads

  1. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  2. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 04:12 PM
  3. for loop not working properly
    By lbgladson in forum New To Java
    Replies: 8
    Last Post: 10-15-2011, 01:33 AM
  4. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •