Sending a message

Sometimes, you don't want a script to start working right from the start of the game. For such cases, you can trigger a script by sending a message, instead of just starting it when the <green flag> button is pressed. This way, the script will stay dormant and inactive until it receives the right message. You can actually compare this to someone yelling a certain order that the script has to respond to. In this section, we will postpone the appearance of the boss monster until certain game conditions are met.

Prepare for lift off

Messages are part of the Events category. Let's go there and see what we have to work with. Near the bottom of the list are three message blocks; one starting block (with the curved top) that can receive a message, and two broadcasters immediately below that can shout a message to all the scripts.

Prepare for lift off

Engage thrusters

We will use these messages to differentiate between the two states of the game. The player can either fight waves of enemies or the boss. Depending on the case, certain scripts have to be made active/inactive:

  1. We go to the Stage script to set up the broadcasts.
  2. First, we throw away the stop <all> block. This is just a temporary means of ending the game.
  3. In its place, we will use the broadcast () block.
  4. We have to create another message using New Message. This can be any kind of word or even a single letter or number:
    Engage thrusters
  5. To make clear what we are calling it, let's name it boss. This message should summon the boss's state in the game.
  6. Before we get there, we also need to summon the waves. So we create another broadcast message.
  7. This will hold a new message named waves.
  8. We place this block right after the reset timer block that signifies the start of the game.
    Engage thrusters

To make use of these broadcasts, someone (or something) has to listen to them. The important sprites that need to respond to the messages are the Starfish enemies and the boss, which are yet to be created:

  1. We click on the Starfish sprite to switch to its Scripts tab.
  2. We detach the script from the when <green flag> clicked block and throw away the block.
  3. In its place, we attach a when I receive <waves> block.
  4. Now this script will wait for the message instead of starting when the <green flag> is clicked on.
    Engage thrusters

To stop and remove the Starfish enemies when the boss phase of the game is reached, we need to add a third script as follows:

  1. Start with another when I receive () block.
  2. We now choose the other message called boss.
  3. When this message is received, we first hide the sprite (using hide).
  4. Then we use stop <other scripts in sprite> to stop the starfish from working.
  5. We can copy this script to the other two starfish enemies.
  6. We also need to replace the when <green flag> clicked block with a when I receive <waves> block, just like the first enemy.

The final script will look like the following screenshot:

Engage thrusters

Objective complete – mini debriefing

When we test the game again, we won't notice any difference. Starting the game will still spawn enemies wave after wave, provided we set up everything properly. If we can survive until the timer reaches 90 seconds, we will see a change. At this point, the boss message will be broadcast. The enemies will respond by disappearing.

The diver can still move about, and the background will keep scrolling. Other than this, nothing exciting happens. We will change that in the rest of this project.

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

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