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

2018/11/21

How do I interrupt a loop after a certain amount of actors have appeared?

DML DML

2018/11/21

#
Apparently, something is wrong about the loop. I am asking you to reply what you think could be wrong. I think, you can ignore which is actually in the loop.
public void steuern()
    {
        int rot = getRotation();
        posY = getY(); posX = getX();
        int actors = getWorld().numberOfObjects();
 
        while(actors <= 2){     [b]//the loop begins here. I want it to be interrupted as soon as 2 actors have appeared[/b]
            while(Greenfoot.isKeyDown("right")){
                turnTowards(posX++, posY);
                if(!markeVorhanden())
                {
                    setzeMarke(); gehe();
                }
                else
                {
                    gehe();
                }}
 
            while(Greenfoot.isKeyDown("left")){
                turnTowards(posX--, posY); 
                if(!markeVorhanden())
                {
                    setzeMarke(); gehe();
                }
                else
                {
                    gehe();
                }
            }
 
            while(Greenfoot.isKeyDown("up")){
                turnTowards(posX, posY--);
                if(!markeVorhanden())
                {
                    setzeMarke(); gehe();
                }
                else
                {
                    gehe();
                }}
 
            while(Greenfoot.isKeyDown("down")){
                turnTowards(posX, posY++);
                if(!markeVorhanden())
                {
                    setzeMarke(); gehe();
                }
                else
                {
                    gehe();
                }}
 
            while(Greenfoot.isKeyDown("n")){
                wachsen(); Greenfoot.delay(1);}
 
            while(Greenfoot.isKeyDown("m")){
                schrumpfen(); Greenfoot.delay(1);}
                 
            if(actors >= 2)
            {
            getWorld().addObject(new Display(),7,0);
            nachricht("Geschafft!"); Greenfoot.delay(5);
            }
        }     [b]//the loop ends here[/b]
 
        getWorld().addObject(new Display(),7,0);
         
    }
danpost danpost

2018/11/21

#
DML wrote...
Apparently, something is wrong about the loop. I am asking you to reply what you think could be wrong. I think, you can ignore which is actually in the loop. << Code Omitted >>
The variable actors is set to the number of actors in the world before the loop (at line 5) and its value never changes during the loop execution.
DML DML

2018/11/23

#
Thank you :) Now it works.
You need to login to post a reply.