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

2021/2/19

Count act rounds

Philinö Philinö

2021/2/19

#
I have to make a method that counts the number of act rounds the camels had to run. But I don't know how to do this. Here's my code:
import greenfoot.*;
public class Buttercup extends Kamel
{
    GreenfootSound sound = new GreenfootSound("bonk2.wav");

    private GreenfootImage Bild1 = new GreenfootImage("Buttercup.png");
    private GreenfootImage Bild2 = new GreenfootImage("walkingB2.png");

    private boolean erstesBild = true;
    public void act() 
    {
        win();
        move();

        Greenfoot.delay(1);

        erstesBild = !erstesBild;
        if(erstesBild) setImage("Buttercup.png");
        else setImage("walkingB2.png");

    }    

    public void win(){
        Actor z = getOneIntersectingObject(Ziel.class);
        World w = getWorld();
        if (z != null){
            Endscreen b = new Endscreen();
            Greenfoot.stop();
            b.setImage("ButtercupWin2.png");
            w.addObject(b, 7, 3);
            Greenfoot.playSound("Won.wav"); 
        }
    }

    public void move(){
        
        
        if((Greenfoot.getRandomNumber(100)<50)){
            setLocation(getX() + 1, getY());
            
            
        }
        if( isTouching(Stein.class)){   
            Greenfoot.delay(2);
            sound.play();
        }    
        if(getOneObjectAtOffset(0, -1, Stein.class) == null && isTouching(Stein.class)){
            Greenfoot.delay(2);
            setLocation(getX(), getY()+1);

        }    
        if(getOneObjectAtOffset(0, 1, Stein.class) == null && isTouching(Stein.class)){   
            Greenfoot.delay(2);
            setLocation(getX(), getY()-1); 
        }    

    }
}
danpost danpost

2021/2/19

#
Add an int field to maintain count of act steps. Increment it in act method. Do not add an Endscreen object into the world if one is already in the world. You could also limit incrementing of the act step count to the same condition. Maybe having the following line as first in act:
if ( ! getWorld().getObjects(Endscreen.class).isEmpty() ) return;
would suffice for both.
You need to login to post a reply.