Time for action - my fortune is what?

What if we get distracted and miss our fortune? That would be tragic, so let's add some code to the teller sprite that repeats the last fortune told:

  1. Create a private variable for the teller sprite named your_fortune. We'll use this variable to keep track of the fortune.
  2. We'll start with the blocks inside the if statement. From the Variables palette, snap the set to block in place between the if block and the say block. Select your_fortune as the variable so that the block reads set your_fortune to 0.
  3. We need to replace the 0 value in the set to block with the code that selects a random item from the answers list. Remember, we're working with the if block right now. Click on the item block and drag the entire block into the to value of the set to block.
  4. From the Variables palette, drag the your_fortune block into the value of the say block. Refer to the following screenshot for the new script:
    Time for action - my fortune is what?
  5. Apply the same changes to the else block. Refer to the following screenshot for help:
    Time for action - my fortune is what?
  6. Get a fortune by pressing the Space bar. Notice that the your_fortune monitor reports the teller's fortune.
    Time for action - my fortune is what?
  7. Now, we need a way to prompt the teller to repeat the message. We'll add a when clicked control block to the teller sprite. From the Control palette, add the when teller clicked block to the scripts area.
  8. From the Looks palette, add the say for 2 secs block to the when teller clicked block.
  9. From the Variables palette, add the your_fortune block as the message value for the say block.
  10. Click the teller sprite to repeat the last fortune.
Time for action - my fortune is what?

What just happened?

Like thoughts, fortunes can be fleeting. That's why we captured our fortune in the your_fortune variable. We replaced where in our script we selected the fortune. Our logic to pick a random item from the answers list became the input to the set to block. And for the say block, we replaced the message value with the your_fortune variable.

Now, if we get a fortune we like, we can make the teller repeat it over and over.

Holding text in a variable

In earlier chapters, we typed text directly into the say block. This means that if we want to change what the sprite says, we have to find the say block in the script and change the message.

When we work with dynamic data, we sometimes want to capture that data so that we can use it later. We already learned how we can use variables to store numeric data, but having the ability to store text adds a whole new level of functionality to our projects, such as simulating dynamic conversations.

Note

Other programming languages often refer to a text phrase as a string.

Variables are one of the most important programming concepts we can use. A majority of the projects you create will need a variable, especially as the projects become more interactive and complex.

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

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