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

2014/9/30

Random Dice

SCollins SCollins

2014/9/30

#
Hi, I am really struggling making some code work, I have tried a few ways and I was wondering if you had any tips. Basically I need to make a small program that displays a dice number up to the amount of sides on a dice. I can do the code to display the random number but I don't know how I could get a different number of sides depending on the die they pick. I have tried mouse info but it seems to think the object is the entire world. Thanks for any suggestions
Super_Hippo Super_Hippo

2014/9/30

#
It depends on how the player picks the die. Basically, you will pass the number of sides as a integer to the constructor of the die class and use this number to create the random number.
danpost danpost

2014/9/30

#
You will need an int field to hold the number of sides picked in the Die class. You should set the value of this field in the constructor of the class, passing the number of sides as a constructor parameter. EDIT: dog-nap-it. Had the page up for a little while and did not check for new entries to this thread.
SCollins SCollins

2014/10/1

#
I have three buttons and when the user presses one I would like it to go to that many sides. I was originally trying things mile mouseClicked and getActor but then didn't work. I will try what you have said but I am not quite sure how to do it, could you give me some example code or a link to somewhere.
Super_Hippo Super_Hippo

2014/10/1

#
Ok, you have three buttons. Do you have an object which creates and displays the random number? And if you click the button, should it display the number or does it change the 'mode' in which it is right now and it displays a number when you click somewhere else?
//in the Button class
private int sides;

public Button(int sides)
{
    this.sides = sides;
}

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        getWorld().removeObjects(getWorld().getObjects( Number.class ));
        getWorld().addObject(new Number(sides));
    }
}
//in the Number class (or however you called it)
private int sides; // in case you don't want to just display it, but maybe need it again later. So if you don't create a new object every time.

public Number(int sides)
{
    this.sides = sides;
    displayNumber(sides);
}

public void displayNumber(int sides)
{
    int s = Greenfoot.getRandomNumber(sides);
    //display the number
}
danpost danpost

2014/10/1

#
All you really need is a field for each of the three buttons and have the act method check to see if one of those buttons were clicked. As a general coding, something like:
private Button buttonOne, buttonTwo, buttonThree;

// after setting button fields with the button objects, you can do the following in the act method
if (Greenfoot.mouseClicked(buttonOne)) // first dice selected
if (Greenfoot.mouseClicked(buttonTwo)) // second dice selected
if (Greenfoot.mouseClicked(buttonThree)) // third dice selected
SCollins SCollins

2014/10/2

#
Thanks danpost and super hippo for the suggestions, dan yours is like what i was trying, but right! I will test the code in a bit and tell you.
You need to login to post a reply.