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

2017/12/15

Dice rolling at the same time?

WardedBowl403 WardedBowl403

2017/12/15

#
I have created two dice and I wish them to both roll when another actor is clicked. This is the code in the dice
1
2
3
4
public dice()
    {
        getImage().scale(300, 300);
    }
And this is the code in the button
1
2
3
4
5
6
7
8
9
public void clicked()
   {
       if (Greenfoot.mouseClicked(this))
       {
           value = Greenfoot.getRandomNumber(6)+1;
           dice d = new dice();
           d.setImage(value + ".jpg");
           d.getImage().scale(300, 300);
       }
danpost danpost

2017/12/15

#
If your die objects are supposed to roll (that is, it is a behavior of a 'dice' object to roll), then you should have a 'roll' method in the 'dice' class. Also, if your die objects are to have values (that is, it is a state of a 'dice' object to have an integer value -- which, in turn, determines its image), then you should have an integer field in the 'dice' class for that value. Methods implement behavior and fields track states of an object. Lines 6 through 8 of the 'clicked' method are pointless as the 'dice' object created is immediately lost once the 'if' clause has finished executing. The only thing that is accomplished when the mouse clicks the button is the 'value' field of the button is given a new random value between 1 and 6, inclusive. You say you have create two dice. Have they been placed into the world? if so, what is the purpose of creating extra 'dice' objects each time the button is clicked? First, give the 'dice' objects their state(s) and behavior(s). Then work with the 'dice' objects that are in the world when the button is clicked. Btw, it is not the state of a button to have a value that is similar to that of a 'dice' object (that is, a 'value' field, as you are using it in the class of the button, does not belong there.
You need to login to post a reply.