Most of us enjoy a good circus, carnival, or county fair. There’s fun, food, and fortunes. Aah, yes, what would a fair be without the fortune-teller’s tent? By the end of this article, you’ll know everything you need to spin your fortunes and amaze your friends with your wisdom.
Before we start the first exercise, create a new project and add two sprites. The first sprite will be the seeker. The second sprite will be the teller. Choose any sprites you want. My seeker will be a clam and my teller will be a snowman. If you want to add a background, go ahead.
Time for action – create a list of questions
In order to have a successful fortune-telling, we need two things: a question and an answer. Let’s start by defining some questions and answers:
- Select the seeker from the list of sprites.
- From the Variables palette, click the Make a list button.
- In the list name dialog box, type questions and select For this sprite only.
- Click OK to create the list. Several new blocks display in the Variables palette, and an empty block titled seeker questions displays on the stage.
- Let’s think about a couple of questions we may be tempted to ask, such as the following:
- Will my hair fall out?
- How many children will I have?
- Let’s add our proposed questions to the questions list. Click the plus sign located in the bottom-left corner of the seeker questions box (on the stage) to display a text input field. Type Will my hair fall out?
- Press the plus sign again and enter the second question: How many children will I have? We now have two questions in our list.
To automatically add the next item in the list, press enter.
- Let’s add a say for 2 secs block to the scripts area of the seeker sprite so that we can start the dialog.
- From the Variables palette, drag the item of questions block to the input value of the say for 2 secs block.
- Double-click on the block and the seeker asks, “Will my hair fall out?“
- Change the value on the item block to last and double-click the block again. This time the seeker asks, “How many children will I have?“
What just happened?
I’m certain you could come up with a hundred different questions to ask a fortune-teller. Don’t worry, you’ll get your chance to ask more questions later.
Did you notice that the new list we created behaved a lot like a variable? We were able to make the questions list private; we don’t want our teller to peek at our questions, after all. Also, the list became visible on the screen allowing us to edit the contents.
The most notable difference is that we added more than one item, and each item corresponds to a number. We essentially created a numbered list.
If you work with other programming languages, then you might refer to lists as arrays.
Because the seeker’s questions were contained in a list, we used the item block to provide special instructions to the say block in order to ask the question. The first value of the item block was position, which defaulted to one. The second value was the name of the list, which defaulted to questions.
In contrast, if we used a variable to store a question, we would only need to supply the name of the variable to the say block.
Have a go hero
Create an answers list for the teller sprite, and add several items to the list. Remember, there are no wrong answers in this exercise.
Work with an item in a list
We can use lists to group related items, but accessing the items in the list requires an extra level of specificity. We need to know the name of the list and the position of the item within the list before we can do anything with the values.
The following table shows the available ways to access a specific item in a list.