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