Results 1 to 6 of 6
Thread: For loop not working properly
- 11-18-2012, 05:38 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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 }
- 11-18-2012, 01:35 PM #2
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.
- 11-18-2012, 06:35 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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; } }
- 11-18-2012, 07:39 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
Re: For loop not working properly
outside that statement it is still looping.
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.
- 11-18-2012, 07:46 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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(); }
- 11-18-2012, 07:59 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
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
Similar Threads
-
If statement is not working properly
By Alpa in forum New To JavaReplies: 3Last Post: 02-04-2012, 08:40 PM -
NetBeans IDE not working properly
By farmer in forum NetBeansReplies: 2Last Post: 12-09-2011, 03:12 PM -
for loop not working properly
By lbgladson in forum New To JavaReplies: 8Last Post: 10-15-2011, 12:33 AM -
date is not working properly
By newnewgen in forum New To JavaReplies: 1Last Post: 10-12-2010, 09:04 AM
Bookmarks