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

2017/3/30

Collision Detection which prompts Textbox ("Question") (with Multiple Choice "Answers")

dawnphilip dawnphilip

2017/3/30

#
Hello. I'm trying to create a collision detection which will promt a GUI (box shaped - 'Question Box'.class) to appear. The 'GUI' should consist of an array i.e. "Q1, Q2, Q3" and for each question, I want 4 multiple choice answers (which will be represented in buttons). Here's my scenario: I have an my main character i.e. 'Owl.class' (which is a picture of an owl that jumps via key press). I have another class which is 'River.class'. I want to make the collision detection work in a way so that ' IF 'owl.class' touch 'river.class' then promot 'QuestionBox'. The question box should ask the user a question, and it should show 4 possible answers, which the user can select.
danpost danpost

2017/3/30

#
dawnphilip wrote...
' IF 'owl.class' touch 'river.class' then promot 'QuestionBox'
In the Owl class act method (or a method it calls), you could have something like this:
1
if (isTouching(River.class)) getWorld().addObject(new QuestionBox(), getWorld().getWidth()/2, getWorld().getHeight()/2);
but, you will need to add another condition, or else questions will keep coming, one on top of another, every act cycle. You could try this:
1
if (isTouching(River.class) && getWorld().getObjects(QuestionBox.class).isEmpty())
for the conditions, but still, when the question is finished and removed, the owl is still touching the river and another QuestionBox object will be instantiated. You will need to add a reference field to the River object the owl is at and control its value so that you do not create multiple QuestionBox objects at the same river:
1
2
3
4
5
6
7
8
9
10
// instance field
private Actor myRiver = null;
 
// in act (or method it calls)
if (myRiver == null && isTouching(River.class))
{
    myRiver = getOneIntersectingObject(River.class);
    addObject(new QuestionBox(), getWorld().getWidth()/2, getWorld().getHeight()/2);
}
else if (myRiver != null && !isTouching(River.class)) myRiver = null;
With this, the owl must be completely off any river before touching another (or the same one, again, if allowed) for the next question to be asked. You may want to place a condition on the execution of the rest of the act cycle for the owl so that the owl is not active during questioning:
1
2
3
4
if (getWorld().getObjects(QuestionBox.class).isEmpty())
{
    // rest of act
}
You can do the same for any other objects in your world that you do not wish to have move around while the owl is being questioned. Now, a question box is not a very easy thing to program; but, do what you can with it, and if, or when, you get stumped on a specific part of it, ask here and show what you tried (post codes to both your Button and QuestionBox classes).
You need to login to post a reply.