Computer Science Course of Study (WA)

 

Events

Page history last edited by Mike Leishman 1 yr ago

Events

Events play a very important part in Scratch. Without events, your program would not start. There are four major event drivers as shown in the diagram below.

 

 

When the flag is pressed

This is the basic starting condition. It is the precursor to the statemetns that can generally be seen as the basis for what is going to happen in the program. You might need to press the flag even if you don't have code attached to it.

 

When a key is pressed

This is a valuable control mechanism in which pressing a key causes some aspect of the program to happen. For example, in the image below, pressing the left and right arrow key will make a sprite move in the direction of the key.

 

 

When a sprite is clicked

This is used to set a set of statements to fire based on the actual sprite. For example, the sprite might be a button that activates a set of statements. (see broadcast). In the example below, the sprite named spaceship will glide to a particular location at a preset speed. Note that variables are used for these values. See http://cscos.pbwiki.com/Variables

 

 

When a message is received

This is a very valuable construct that allows the control of other sprites by a given sprite. In this case, the transmitting sprite sends a broadcast message. The receiving sprite must "listen" for the message designated for it and, when received, act on it.

 

In the case below, the scene is started by clicking the Green flag that enables a message to be broadcast that tells those sprites listenting to hide themselves. In this case, the cat sprite but it could be many other sprites as well.

 

For the "Title" sprite, it is also told to show itself.

 

The title sprite also listens for a "hide title" message. In the event the title receives a message to hide itself, it does so.

 

 

When the cat button is pressed, it sends out three messages

 

  1. Hide the title
  2. Hide other sprites that are listening
  3. Show the Cat sprite

 

 

The cat sprite waits for messages to be recieved.

 

  1. The "hide all" message tells the sprite to hide itself
  2. The "show cat" message tells the cat to show itself

 

 

Exercises

  1. Write an program that uses an event to makes a sprite jump around the stage when the green arrow is pressed.
  2. Modify the program above to use the event of the sprite being clicked.
  3. Write a program that uses an event that makes a rocket "fire" from a launcher when the spacebar is pressed.
  4. Write a set of procedures that allows two different sprites to appear on the stage by the click on the relevant button.

 

Comments (0)

You don't have permission to comment on this page.