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

2013/12/2

set image random??

sgt12 sgt12

2013/12/2

#
How to make image random when first run?
Super_Hippo Super_Hippo

2013/12/2

#
Could you be a bit more specified? Randomly take one image out of some or take a random color / random pixel for the image? No one can help you if you just use such a sentence.
sgt12 sgt12

2013/12/3

#
I want to make a random object (from random class) in the first run. It's like I have banana class, apple class, grape class and I want one of three class showed up (random) in the first run.
sgt12 sgt12

2013/12/3

#
need some help
Super_Hippo Super_Hippo

2013/12/3

#
In your first post, you wanted to make an image random. Now you want to create a random class. For your second question, you can write something like this in the code in your world class where you create the classes: (Of course you need to change x and y to the numbers you want.)
        switch( Greenfoot.getRandomNumber(3) )
        {
            case 0: addObject(new Banana(), x, y); break;
            case 1: addObject(new Apple(), x, y); break;
            case 2: addObject(new Grape(), x, y); break;
        }
For the first question, you can just create for example a fruit class:
addObject(new Fruit(), x, y);
And then, in this Fruit class, you set the image to one the fruits. This is useful if all these fruits are the same in your game except their images:
Super_Hippo Super_Hippo

2013/12/3

#
I forgot the other code - BB-code....
public Fruit()
    {
        switch( Greenfoot.getRandomNumber(3) )
        {
            case 0: setImage("banana.png"); break; //or whatever your image files are named
            case 1: setImage("apple.png"); break;
            case 2: setImage("grape.png"); break;
        }
    }
You need to login to post a reply.