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:

  1. Select the seeker from the list of sprites.
  2. From the Variables palette, click the Make a list button.
  3. In the list name dialog box, type questions and select For this sprite only.
  4. 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.
    Time for action - create a list of questions
  5. 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?
  6. 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?
  7. Press the plus sign again and enter the second question: How many children will I have? We now have two questions in our list.

    Tip

    To automatically add the next item in the list, press enter.

  8. Let's add a say for 2 secs block to the scripts area of the seeker sprite so that we can start the dialog.
  9. From the Variables palette, drag the item of questions block to the input value of the say for 2 secs block.
  10. Double-click on the block and the seeker asks, "Will my hair fall out?"
  11. 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?"
Time for action - create a list of questions

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.

Note

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. We saw those examples in Chapter 6.

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.

Position

Description

Uses

First

Identifies the first item in the list.

Insert, delete, replace, or retrieve the first item in the list.

Any

Selects a random item in the list.

Insert, replace, or retrieve a random item in the list.

Last

Selects the last item in the list.

Insert, delete, replace, or retrieve the last item in the list.

Variable

Specifies a variable that contains a number instead of the default first, any, or last values.

Use the variable to store the position of an item in the list, then insert, delete, replace, retrieve the item that corresponds to the value.

Manual input

Enters a specific item number.

Insert, delete, replace, or retrieve a constant item number, such as item 5.

Import a list

Entering one item at a time via the Scratch interface is functional, but the small size of the list box can be difficult to use when we need to add a large number of items. Fortunately, we can create a text file outside of Scratch and then import it into our list.

Before you continue with the exercise, build your own text file with the fortunes you want to use in response to the seeker's questions. Enter one fortune per line in a text editor, such as Notepad.

Import a list

You can use any fortunes you want, but I'm going to use a list of common Magic 8 ball responses that I found on Wikipedia at http://en.wikipedia.org/wiki/Magic_8_ball. My list will contain 19 items to start.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset