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

2014/5/16

While loop in a world subclass crashes Greenfoot

sp99 sp99

2014/5/16

#
I have a while loop in my main gameboard class that should constantly check (until the game finishes) if two counters are on the same y axis. Here is my while loop:
while(finished == false) {
            if(cr1.sameSpace() == 1) cb1.setLocation(cb1.getX(), 385);
            if(cr2.sameSpace() == 2) cb2.setLocation(cb2.getX(), 385);
            if(cb1.sameSpace() == 1) cr1.setLocation(cb1.getX(), 385);
            if(cb2.sameSpace() == 2) cr2.setLocation(cb2.getX(), 385);
            if(cr1.sameColourOnSpace() == true)
            {
                System.out.println("Hello12349999");
                cr1.setLocation(cr1.getX(), cr1.yPos);
                cr1.act();

            }
        }
There will be four of the final if statement (one for each counter) and sameColourOnSpace() ans sameSpace() are methods in each counter class.
public int sameSpace() {
        Actor cb1OnSpace = getOneObjectAtOffset(100, 0, CB1.class);
        Actor cb2OnSpace = getOneObjectAtOffset(140, 0, CB1.class);
        Actor cb12OnSpace = getOneObjectAtOffset(50, 0, CB1.class);
        Actor cb22OnSpace = getOneObjectAtOffset(50,0, CB1.class);

        if (getY() != 385 && getY() != 237 && getY() != 12) {
            if (cb1OnSpace != null || cb12OnSpace != null) return 1;
            if (cb2OnSpace != null || cb22OnSpace != null) return 2;
        }
        return 0;
    }
public boolean sameColourOnSpace() {
        Actor otherCounterOnSpace = getOneObjectAtOffset(50, 0, CR1.class);
        Actor otherOtherCounterOnSpace = getOneObjectAtOffset(-50, 0, CR1.class);

        if ((otherCounterOnSpace != null || otherOtherCounterOnSpace != null) && getY() != 385 && getY() > 12 && getY() != 237)
        {
            return true;
        }
        else {
            return false;
        }
    }
davmac davmac

2014/5/16

#
while(finished == false) {  
This looks like an infinite loop. I don't see you set finished = true anywhere...
sp99 sp99

2014/5/16

#
It's not infinite, I set it in another point of the program. But I may have fixed it now thanks.
Super_Hippo Super_Hippo

2014/5/17

#
It is infinite since the program will never leave this loop. The 'loop' you want is probably the 'act' method. This is executed every act-cycle.
You need to login to post a reply.