0

I have 5 rectObj yet the loop is only continuing twice.

Here's my code:

public boolean bottomLeft() {
    boolean ret = false;
    Iterator<rectObj> rectItr = Main.rectAr.iterator();
    while (rectItr.hasNext() && ret == false) {
        rectObj e = rectItr.next();
        if (x > e.getX() && x < e.getX()+e.getWidth()
                && y+h > e.getY() && y+h < e.getX()+e.getHeight()) {
            ret = true;
        }else{
            if(rectItr.hasNext()) {
                rectItr.next();
            }
        }
    }
    return ret;
}

rectObj rect1 = new rectObj(250,250,50,50);
    rectObj rect2 = new rectObj(0,440,500,50);
    rectObj rect3 = new rectObj(0,0,500,50);
    rectObj rect4 = new rectObj(400,200,50,50);
    rectObj rect5 = new rectObj(0,200,50,50);

    rectAr.add(rect1);
    rectAr.add(rect2);
    rectAr.add(rect3);
    rectAr.add(rect4);
    rectAr.add(rect5);

I want the loop to continue throughout all of the array unless it finds an object that it has collided with.

Thanks for any replies!

2 Answers 2

2

You're calling rectItr.next() a second time in the else part inside the loop. Try removing the else part of the if-else statement.

The else part is checking if rectItr.hasNext() and if it has, it is calling rectItr.next(). Then it returns to the top of the while loop which is doing the same thing again. Therefore, each loop, if the if test is false, then it calls rectItr.next() an extra time, and skips the next element in the array.

Sign up to request clarification or add additional context in comments.

2 Comments

I removed the next statement and the collision was detected for another rectObj but removed for one that was working with the next satement
Ah, I removed the if statement completely and now it works great! Thank you very much!
0

@Dermot is correct.

You can actually simplify and reformat the method as follows:

public boolean bottomLeft() {
    for (rectObj e : Main.rectAr) {
        if (x > e.getX() 
                && x < e.getX() + e.getWidth()
                && y + h > e.getY() 
                && y + h < e.getX() + e.getHeight()) {
            return true;
        }
    }
    return false;
}

... and this reveals that there is ANOTHER bug in your method. This line

      && y + h < e.getX() + e.getHeight()

... should be ...

      && y + h < e.getY() + e.getHeight()

... I think.

2 Comments

Thanks for pointing out my mistake! However "Can only iterate over an array or an instance of java.lang.Iterable"
@StephenC I saw a very interesting talk/blog post a while back which had a specific note that when you have sections of code which are largely copied, but with small differences, there is overwhelmingly often a bug in one of the later copies just like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.