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

2021/2/18

Probability

Philinö Philinö

2021/2/18

#
I am trying to write a Method, so that the automatically moving camels have a 50% chance to move forward (per act), but I don't know how to do it. Here's my code:
import greenfoot.*;

public class Buttercup extends Kamel
{

    GreenfootSound sound = new GreenfootSound("bonk2.wav");

    public void act() 
    {
        win();
        move();

        Greenfoot.delay(1);

    }    

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

    public void move(){

        if (isTouching(Stein.class)){
            if((Greenfoot.getRandomNumber(100)<50) && getOneObjectAtOffset(1, 0, Stein.class) == null){   
                
                    setLocation(getX() + 1, getY());

                
            }    
            sound.play();
            setLocation(getX(), getY()+1); 
            setLocation(getX(), getY()-1); 

        }


    }
}
for some reason, the method (Greenfoot.getRandomNumber(100)<50) is not working.
danpost danpost

2021/2/18

#
Looks like line 31 restricts movement to only when touching a Stein object. Lines 39 and 40 counter each other (no resultant change in vertical position by those lines). It is as if those lines do not exist.
Philinö Philinö

2021/2/18

#
@danpost that was the error, now it's working. thank you!!
You need to login to post a reply.