lundi 22 mai 2017

Java - How can I assign one of three images randomly to an actor in Greenfoot?

I have an actor Pets, which I want to randomly select 1 of 3 images to use when it is placed. I think this can be done using Math.Random and Math.Floor, but I can't get it to work correctly-

public class Monster extends animals
{
    private GreenfootImage img1;
    private GreenfootImage img2;        
    private GreenfootImage img3;  

    public Monsters() {
        int x = 0;
        int y = 4;
        img1 = new GreenfootImage("tyui.png");       
        img2 = new GreenfootImage("lkjh.png");       
        img3 = new GreenfootImage("asdf.png");   

        if (Math.floor(Math.random() * ((y-x)+1)+x) = 1){
           setImage(img1);
        }else if (Math.floor(Math.random() * ((y-x)+1)+x) = 2){
           setImage(img2);
        }else if (Math.floor(Math.random() * ((y-x)+1)+x) = 3){
           setImage(img3);

    }




Aucun commentaire:

Enregistrer un commentaire