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

2023/1/20

How to detect if all objects are gone

Psg Psg

2023/1/20

#
As of right now I have code that randomly places an object somewhere on the screen. I want the whole code to stop when all of them are removed
danpost danpost

2023/1/20

#
Psg wrote...
As of right now I have code that randomly places an object somewhere on the screen. I want the whole code to stop when all of them are removed
Let me call the subclass of Actor that all may be removed from the world Zombie, for example (just to give it a name). Then, in MyWorld class or, rather, in World subclass representing the game itself:
public void act()
{
    if (getObjects(Zombie.class).isEmpty()) Greenfoot.stop();
}
Psg Psg

2023/1/20

#
The code after greenfoot is cut off, but if is the greenfoot stop, that is what I have. Should it be in the world class or the actor class
Psg Psg

2023/1/20

#
here is my code:
public void Worm(){ // automaticalay randomize different placed worms on screen
        while( i < 10){
            x = Greenfoot.getRandomNumber(560);
            y = Greenfoot.getRandomNumber(560);
            Worm W1 = new Worm();
            addObject (W1,x,y);
            i++;
        }
    }
    public void Stop(){ 
         if(getObjects(Worm.class).isEmpty()){
            Greenfoot.stop();
        }
    } 
Psg Psg

2023/1/20

#
the second part is what is not working
danpost danpost

2023/1/20

#
Psg wrote...
The code after greenfoot is cut off, but if is the greenfoot stop, that is what I have. Should it be in the world class or the actor class
danpost wrote...
in MyWorld class or, rather, in World subclass representing the game itself:
public void act()
{
    if (getObjects(Zombie.class).isEmpty()) Greenfoot.stop();
}
Must be in the act method of world or in a method called from the act method of world.
Psg Psg

2023/1/20

#
it worked. Thank you!
Psg Psg

2023/1/21

#
hey Dan. I have another question. Your code is working properly, but what should I do?
public void act(){//stop the game when all worms are eaten or crab is removed
        List numL= getObjects(Lobster.class);
        if (getObjects(Worm.class).isEmpty()){
             removeObjects(numL);
             List numC= getObjects(Crab.class);
             removeObjects(numC);
             setBackground("win.png");
             Greenfoot.stop();   
        }
        if (getObjects(Crab.class).isEmpty()){
            removeObjects(numL);
             List numW= getObjects(Worm.class);
             removeObjects(numW);
            setBackground("lose.png");
            Greenfoot.stop();
        }
    }
Psg Psg

2023/1/21

#
the second if statement always activates because the first always creates an empty crab class
danpost danpost

2023/1/21

#
Psg wrote...
what should I do? << Code Omitted >> the second if statement always activates because the first always creates an empty crab class
Put the following statement:
return;
at the end of both if blocks.
Psg Psg

2023/1/26

#
hey, dan I need your help. I have a problem. I believe this if statement is working, but reading the wrong value of the integer rotate. What should i do
private int rotate=0;
public void checkEdge(){ //see if the Lobster is at the edge
        if(isAtEdge()){
            rotate=rotate+90;
            setRotation(rotate);
            turn(-15);
        }
public void turnCrab(){//constantly turn the Lobster in a small area
         if(one==false){
            turn(15);
            y=getY();
            one=true;
        }
        x=getY();
        if(rotate%180!=0 && x==y+20){
            turn(15);  
        }
danpost danpost

2023/1/27

#
Psg wrote...
hey, dan I need your help. I have a problem. I believe this if statement is working, but reading the wrong value of the integer rotate. What should i do
First, you should understand that the rotation is already being held in the Actor class, meaning you do not need to declare another field for it. You could use something like the following:
setRotation(getRotation()+90);

// or just
turn(90);
It is not really clear by your code as to exactly what you are trying to achieve with the turn methods Obviously, you want the crab to turn sharp at an edge, which can simply be accomplished with:
if (isAtEdge) turn(90);
The turnCrab method is totally unclear. There is no code shown where the value of one is turned back to false. And line 15 seems a bit strange as it appears the turn will only occur when the rotation is not horizontal (exactly left or right) and the turn must only occur along a distinct diagonal line going 45 degrees downward starting just below the top-left corner of the scenario window. I doubt very much that this is what you were going for.
You need to login to post a reply.