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

2014/4/12

How to make a text box whit questions and response options

BasseQ47 BasseQ47

2014/4/12

#
hey i am making a game for school project. And need help to make a text box that contains a question, then it shut have some small buttons, that will allow the player to answer the questing. and if he gets it right the main actor will move one step forward and if he's wrong when it's game over. i have not been able to find some other discussions about this. so i am i need of help
danpost danpost

2014/4/13

#
Will this be like multiple choice where each button is associated with a possible answer, or will the player have to enter (type) in the correct answer? Of course, multiple choice would be much easier to code. Do you want to use the JOptionPane class or Actor objects for your text box and buttons?
BasseQ47 BasseQ47

2014/4/13

#
yes the player is giving 3 different options there only one of them are the right answer. and if i pick the right one then the main actor will move closer to the goal. but if he don't get a correct answer when it's game over. i am not familiar whit JOptionPlane so i think that actor objects will be the save way to go thank you for the response
danpost danpost

2014/4/13

#
The world class should then have a method to 'pose' a question. It would create a Question actor to display the question and create an array of Button actors for the options. You will need an instance Actor field to hold the correct button actor. Keep it 'null' when a question is not being posed so the world act method can know when not to check for a button click. It would be asking something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (correctButton != null && Greenfoot.mouseClicked(Button.class))
{
    if (Greenfoot.mouseClicked(correctButton))
    {
        removeObjects(getObjects(Question.class));
        removeObjects(getObjects(Button.class));
        mainActor.move(1);
        correctButton = null;
    }
    else
    {
        Greenfoot.stop();
        // any other game over actions
    }
}
BasseQ47 BasseQ47

2014/4/13

#
okay let say if i want 5 questions through the hole when i have to make 5 actor classes there each hole one of the questions. and under thesis questions actors i'll make 3 buttons classes there one of then is named correct button
danpost danpost

2014/4/14

#
You only need one Question class and one Button class. Use a double String array to hold the all the String objects:
1
2
String[][] problems = { { "Q 1", "Opt 1", "Opt 2", "Opt 3" },
                        { "Q 2", "Opt 1", "Opt 2", "Opt 3" } /** etc. */  };
and an int array to hold the answers:
1
int[] answers = { 3, 1 /** etc. */ };
The above fields can be static and final as they will be constants. Next, you will need an int field to hold the index of the current problem. The world will choose a valid index value, create one Question actor to display the question and the three Button actors to display the options. When creating the button that displays the correct answer, set the 'correctButton' field to that button.
BasseQ47 BasseQ47

2014/4/25

#
okay thats looks very good but i am having trouble making it work this id my world class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.util.List;
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class world extends World
{
    GreenfootImage background = getBackground();
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
         super(700, 500, 1);
         
        addObject(new MoveingMan() , 200 , 380);
       
    }
    if (correctButton != null && Greenfoot.mouseClicked(Button.class)) 
    
    if (Greenfoot.mouseClicked(correctButton)) 
    
        removeObjects(getObjects(Question.class)); 
        removeObjects(getObjects(Button.class)); 
        mainActor.move(1); 
        correctButton = null
    
    else 
    
        Greenfoot.stop(); 
        // any other game over actions 
    
    
 
    }
}
and this is my main actor class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class man here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MoveingMan extends Actor
{
    /**
     * Act - do whatever the man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       if (Greenfoot.isKeyDown("z"))
        {
            getWorld().addObject(new Questing_1(), 320,250);
        
       
   
}
}
i can not under stand how that you will create the buttom class and the questing class i am hopeing for a quick response because i have only 10days left to finish in
danpost danpost

2014/4/25

#
First, it is the world that will be creating controlling the questioning and the moving of the MoveingMan object; so, move the act method from the MoveingMan class to the world class (and remove 'getWorld' from the 'addObject' line). Next, I do not see where you declare the 'correctAnswer' field in the world class. Add the following at line 13:
1
private Actor correctAnswer;
Now, you do not want to be able to create multiple Questing_1 objects at the same time. So, we need another condition on the 'z' pressing 'if' in the act method. Use
1
if (correctAnswer == null && Greenfoot.isKeyDown("z"))
Finally, within the 'if' block, you will need to also add the buttons and set 'correctActor' to the appropriate one (unless some of that is done during the construction of the new Questing_1 object). You may want to post the Questing_1 class for further help.
danpost danpost

2014/4/25

#
One more thing -- lines 25 through 39 in your world class should be inside the act method of that class.
BasseQ47 BasseQ47

2014/4/27

#
okay that got me a bit cloaser to my goal but not to finish to do that i might need some more help this is now my world class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.util.List;
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class world extends World
{
    GreenfootImage background = getBackground();
    private Actor correctAnswer;
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1);
         
        addObject(new MoveingMan() , 200 , 380);
        if (Greenfoot.isKeyDown("v"))
        {
           addObject(new buttomEorror_1(),380,380);
        }
           if (correctButton != null && Greenfoot.mouseClicked(Button.class)) 
        
            if (Greenfoot.mouseClicked(correctButton)) 
            
         removeObjects(getObjects(Question_1.class)); 
         removeObjects(getObjects(buttonEorror_1.class)); 
         MoveingMan.move(1); 
         correctButton = null
         
        else 
         
         Greenfoot.stop(); 
         // any other game over actions 
         
        
    }
        public void act()
    {
       if (correctAnswer == null && Greenfoot.isKeyDown("z"))
        {
            addObject(new Questing_1(), 320,250);
        
    }
}
and here is Questing_1 class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Instructions here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Questing_1 extends Actor
{
    private GreenfootImage Questing_1 = new GreenfootImage("Questing_1.png");
    public void act()
    {
 
        if (Greenfoot.mouseClicked(this))
        getWorld().removeObject(this);
         
    }   
}
i hope that's this not gonna be to complicated thank you for your help
danpost danpost

2014/4/27

#
At the moment, you have no need to import 'java.util.List' or 'java.awt.Color' into the 'world' class. Remove lines 2 and 3. Also, you are not using the 'background' field anywhere in the 'world' class. So, remove line 12. Now, you only move a line or two of what I said to move to the 'act' method of the 'world' class. You need to move everything between 24 and 42, inclusive, into the 'act' method. Question: what is pressing the 'v' key supposed to do? or maybe I should ask, what is the buttomEorror class creating and what is the purpose of that actor?
BasseQ47 BasseQ47

2014/4/28

#
i have know moved it all in to my world class. the v key was just to test the code that i had in the buttomEorror_1 class the buttomEorror_1 class is one of my buttons it has none i have 3 driffent calsses for buttons there one of them are called corretButtom this is my world class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class world extends World
{
    private Actor correctAnswer;
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1);
         
        addObject(new MoveingMan() , 200 , 380);
    }
        public void act()
    {
       if (correctAnswer == null && Greenfoot.isKeyDown("z"))
        {
            getWorld().addObject(new Questing_1(), 320,250);
        
            {     
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels. 
         super(700, 500, 1);  
           
        addObject(new MoveingMan() , 200 , 380); 
         
       
       if (correctButton != null && Greenfoot.mouseClicked(Button.class))   
       {   
       if (Greenfoot.mouseClicked(correctButton))   
       {   
        removeObjects(getObjects(Question.class));   
        removeObjects(getObjects(Button.class));   
        mainActor.move(1);   
        correctButton = null;   
        }   
       else   
        {   
        Greenfoot.stop();   
        // any other game over actions   
       }   
       }   
    
}
    
i am not sure that i complete under stand how you want to make the buttons and the questing classes
BasseQ47 BasseQ47

2014/4/28

#
You need to login to post a reply.