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

2016/5/30

Help with Educational Project

chickenugget chickenugget

2016/5/30

#
So I'm creating a project in which there's a question such as (100km = 100,000___) and the person has to select the answer from 4 buttons I've set up. mm, cm, m and km. I made all the 4 buttons as an actor class, made the question an actor class too. How do I make the buttons react to the question now though?
danpost danpost

2016/5/30

#
Buttons should remain fairly dumb in that they should only react to mouse actions, if anything -- altering their image depending on what the mouse is doing (mouse hovering state or mouse button pressed state). The world is what should control the main actions of the simulation -- checking for clicks on buttons currently in the world and responding to them. The world should also hold a reference to the correct button that answers the current question. Let us say you have a Text class whose objects can display the questions as they are posed and a Button class whose objects can display the possible answers to the question. Then, in the world class, you would have:
// instance field
private Actor correctButton; // to hold correct answer button

// creating a question
Text question = new Text("100km = 100,000___");
addObject(question, << wherever >> );
Button button = new Button("mm");
addObject(button, << wherever >> );
button = new Button("cm");
addObject(button, << wherever >> );
button = new Button("m");
addObject(button, << wherever >> );
correctButton = button; /** saves the correct answer button */
button = new Button("km");
addObject(button, << wherever >> );

// in act method
if (correctAnswer != null && correctAnswer.getWorld() == this) // ensures a question is posed
{
    for (Object obj : getObjects(Button.class))
    {
        if (Greenfoot.mouseClicked(obj))
        {
            if (obj == correctAnswer)
            {
                // correct button clicked
            }
            else
            {
                // wrong button clicked
            }
        }
    }
}
You need to login to post a reply.