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

2011/7/15

how do you pick a random costume

nooby123 nooby123

2011/7/15

#
How do you pick a random costume?
DonaldDuck DonaldDuck

2011/7/15

#
Well... Strange question, but I suppose you would get a random number and if it is (x) then assign costume (x)
int costume = Greenfoot.getRandomNumber(the number of costumes you have);
if(costume==1)
{
    setCostume(a costume);
}
if(costume==2)
{
    setCostume(a costume)
}
etc., etc., etc.
nooby123 nooby123

2011/7/15

#
thanks
mik mik

2011/7/19

#
Two remarks: First, random numbers start with 0 (not 1). So for your first costume you have to compare to 0. Second, you could save the sequence of if statements by using an array, and using the random number as an index into that array. For example:
String [] costumes = { "image1.png", "image2.png", "image3.png" );

...

int index = Greenfoot.getRandomNumber(costumes.length);
setImage(costumes[index]);
You need to login to post a reply.