This site requires JavaScript, please enable it in your browser!
Greenfoot back
JChapel
JChapel wrote ...

2013/10/28

Boolean not returning inside a for-nested if

JChapel JChapel

2013/10/28

#
Here is the method affected by the gremlins. I'm attempting to go through a range of Y values for more precise collision detection, but it only seems to return code outside of the for loop. Tested without the for loop earlier, with 0 replacing bumpY, worked fine.
public boolean turn()
    {
       World myWorld = getWorld();
       int bumpY;
       Actor bumpright;
       Actor bumpleft;
       for(bumpY = -10; bumpY == 10; bumpY++) {
           bumpright = getOneObjectAtOffset(10, bumpY, Bumper.class);
           bumpleft = getOneObjectAtOffset(-10, bumpY, Bumper.class);
           if (bumpright != null && getRotation() == 0) {
               setRotation(180);
               return true;
           }
           else if (bumpleft != null && getRotation() == 180) {
               setRotation(0);
               return true;
           }
           else if(getX() >= myWorld.getWidth() - 10 && getRotation() == 0) {
               setRotation(180);
               return true;
           }   
           else if (getX() <= 10 && getRotation() == 180) {
               setRotation(0);
               return true;
           }
       }
       return false;
    }
JChapel JChapel

2013/10/28

#
It's also is worth noting that the rotation code inside the ifs isn't executing either.
danpost danpost

2013/10/28

#
Your condition for executing the 'for' loop will be 'false' from the start as 'bumpY' will be '-10' (not '10'). So, the loop never executes.
JChapel JChapel

2013/10/28

#
Ah, I misread the for loop tutorial code then, took it to mean until true. It works now, thanks.
You need to login to post a reply.