Questions using events By Deborah Nelson Duke University Under the direction of

advertisement
Questions using events
By Deborah Nelson
Duke University
Under the direction of
Professor Susan Rodger
July 31, 2008
Loading the world
•
•
•
•
Download the starting world
It is called questionEventStart.a2w
Save it in a directory that you can find again,
After you have opened the file go into the
"Layout" mode by clicking on the green button
Add Objects (toward the middle of screen)
• Click more controls. Click drop a dummy at the
camera. Rename the dummy 'originalPosition.'
To leave the layout mode, click done
Overview
• The purpose of this tutorial is to provide a
template for a quiz in which the user clicks on
a different object to answer each question.
• We are going to create the following in this
tutorial:
– Two world-level property variables
– Three world-level methods
– One world-level function
Creating property variables
• In the world details pane, click ‘create new
variable’
• Name it ‘correctAnswer’. Select type Object
• Click ‘create new variable’ again
• Name this one ‘userAnswer’ select type
Object
• These variables will be used in each of the
methods we are going to write.
Quiz Method
• Now, create a new world method, name it
‘quiz1’.
Part 1: Writing quiz1 method
• In this example world, the teacher asks each
question.
• Step 1: For the first question, click on the
teacher in the object tree. Drag the teacher
say method into the quiz1 method, select
other and type in the question ”Which animal
says ‘meow’?:
• The next step is to store the correct answer in
our correctAnswer variable. See the next slide
Storing the correct answer
• Step 2: Click on the properties tab in the world
details pane. Drag “correctAnswer” into the
“quiz1” method
• In the drop down menu, select – “set value”,
“cat”, “the entire cat”
• See the screenshot on the next slide for an
illustration
• Resulting Code:
• Step 3: Drag a while from the bottom of the
window into your quiz1 method
• In the drop down menu, select true
• Click on the properties tab in the world details
pane
• Drag correctAnswer on top of the true
• Step 4: In the drop down menu, select
“correctAnswer !=“, expressions,
world.userAnswer
– != means ‘does not equal’
See the screenshot on the next slide for an
illustration
• Resulting code
• Step 5: Finally, drag a wait from the bottom of
the window at the end of quiz1 method
• Select 2 seconds in the drop down menu
• Repeat Part 1,ssteps 1-5 for question 2:
“Which animal hops?”
• The answer is bunny.
 Your resulting code should look like the
screenshot on the following slide.
Calling Quiz1
• Click on the world.my first method tab.
• Click on the methods tab in the details pane.
Drag quiz1 into world.my first method
Playing your world
• Click on the properties tab in the world details
• Play your world. The ‘correctAnswer’ variable
changes to cat
Part 2: Storing the user’s answer
• We will write code to store the user’s answer
• Click on the function tab, create a new
function, name it ‘possibleChoices’, select type
Boolean. Which returns true or false
Create a new parameter
• Click on “create new parameter” in the
function
• Name it ‘objClicked’. Select type “Object”
• Step 1:Drag an “if/else” from the
bottom of the window into the
function. Select true in the drop
down menu
• Step 2: Drag the parameter
“objClicked” onto the true. In the
drop down menu, select
“objClicked ==“, cat, the entire cat
• See the screenshot on the next
slide for an illustration
Resulting code:
• Step 3: drag Return from the bottom of the
window on top of the Do Nothing under the If
• Click on the <None> beside return and select
true
• Step 1: Drag an if/else
on top of the Do
Nothing under the Else
• Select true in the drop
down menu
• Step 2: Drag the
parameter objClicked
on top of the true. And
select ==, bunny, the
entire bunny.
Resulting code:
• Step 3: Drag in
“Return.” Select true.
• Repeat Part 2, steps 1-3
for T-rex.
• Repeat Part 2, steps 1-3
for cow.
• The rest of your nested
if statements should
look like this:
• Drag Return on top of
the final Do Nothing.
Select false in the drop
down menu
• Don’t forget to set the
final Returnto false.
Compare method
• In the world details pane, click on the method
tab. Create a new method. Name it ‘compare’
• Drag an if/else from the bottom of the
window. Click on the functions tab and drag
the function possibleChoices on top of the
true.
• Click on the properties tab in the world details
pane
• Drag ‘userAnswer’ on top of the <None>
• Drag another if/else on top of the ‘Do
Nothing’ underneath the ‘if’
• Resulting Code:
• Drag the ‘userAnswer’ property from the
properties pane, on top of the ‘Do Nothing’
underneath this second If.
• Select the ‘say’ method. Type “I am not the
correct answer”
The else
• Drag a ‘do together’ from the bottom of the
window into the ‘Do Nothing’ underneath the
Else
• Drag the teacher ‘say’ method into the code.
Select other and type “Good job. That’s right”
• Drag the ‘userAnswer’ into the do together
and select turn left 1 revolution.
See the screenshot on the next slide for an
illustration
• In the final ‘Else’, drag the teacher ‘say’
method into it. Select ‘other’ and type “click
on the object to answer the question”
• Change the duration to 1.5
See the screenshot on the next slide for the
complete method
ChooseObject method
• Next, write the method to call this compare
method. ‘Create a new method’ in the world
details pane.
• Name it ‘chooseObject’
• ‘Create a new parameter’ in this method.
Name it ‘objClicked’. Select type ‘object’
• Click on the properties tab in the world details
pane. Drag ‘userAnswer’ into the
‘chooseObject’ method. Select ‘set value’,
‘expressions’, ‘objClicked’
• Click on the methods tab. Drag compare into
the chooseObject method
• This is the complete method
Calling the chooseObject method:
an event
• In the events editor, click on “create new
event”, select ‘when the mouse is clicked on
something’
• Click on the methods tab in the world details
pane.
• Drag it on top of the “Nothing” in the event
• In the drop down menu,
select “expressions”,
“object under mouse
cursor”
• Here is the complete
event:
Play your world
• After the teacher asks the first question, click
on the cat for your answer to make sure all of
your methods work
• To test all of your if statements, after the
teacher asks the second question, click on a
wrong answer. Then click on anything in the
world that is not an animal.
Practice
• The steps in this tutorial are useful for
answering questions with the click event.
• Go back to quiz1 method. Using steps 1-5
from Part 1 of this tutorial, ask these two
questions:
-Which animal is extinct? (the T-Rex)
- Which animal can be milked? (the cow)
Further practice
• Drag the camera set point of view instruction
into your world.my first method. Select
Dummy Objects, Quiz2View
• Create a new world method named quiz2.
• In quiz2, ask questions about each of the
shapes using the same format as quiz1.
– Keep in mind that you will also have to revise your
possibleChoices function in order to include the
shapes
Recap
• Two world-level property variables are used to store
the correct answer and the user’s answer
• To begin, the correct answer is set. Until the user’s
answer equals the correct answer, an empty while
loop keeps the next question from being asked
(world.quiz1)
• When the user clicks on an object, a method is called
to store that object as their answer
(world.chooseObject), as long as they clicked on one
of the choices (world.possibleChoices)
• Then, a method is used to compare the user’s
answer to the correct answer (world.compare)
Download