<< Chapter < Page Chapter >> Page >

A moonscape background from the gallery was used for the project shown in Image 4 .

Controlling the behavior of a sprite

You control the behavior of a sprite by writing one or more scripts that belong to the sprite. In reality, each script is an event handler ofsorts. Usually you will have one event handler that executes when the user clicks the green flag at the top right in Image 6 . This is particularly true if any special initialization code is required for the sprite, such assetting the current costume to a particular costume, for example.

In addition to the green-flag script, you can also write scripts to handle mouse events, key events, and a special type of event that fires when one spritebroadcasts a message to one or more other sprites. Therefore, Scratch projects are not only sprite-based; they are also event-based. These arerelatively advanced concepts in most programming environments. However, Scratch was designed in such a way as to make it easy to write event-handlerscripts to control the behavior of sprites.

Drag and drop programming

Creating Scratch programs involves very little typing. Instead, scripts are created by constructing stacks of blocks, where each block imparts somespecific behavior to a sprite or to the stage. For example, the project being developed in Image 6 has a stage, (which is always the case) but doesn't yet have any sprites. The stage is represented by the rectangularthumbnail image in the bottom right area of Image 6 . Any sprites that are added to the project would also appear as thumbnail images in that same area.I will refer to this area as the sprite list area.

The physical process for writing a script is as follows:

  1. Select the stage or a sprite in the sprite list area that is to be programmed.
  2. Select the Scripts tab in the center pane.
  3. Select one of the color-coded buttons in the toolbox button area at the top left to expose the blocks contained in a particular toolbox in thetoolbox pane. The toolbox pane is the large pane on the left.
  4. Drag blocks from the toolbox to the scripts (center) pane. (Several blocks have already been dragged to the scripts pane in Image 6 .)
  5. Go back to 3 and continue this process until you have all of the blocks that you need in the scripts pane.
  6. Snap the blocks together in the correct arrangement to create a script that produces the desired behavior.

You will learn the details of such operations in future modules.

Costumes and sounds

Selection of the other two tabs showing at the top of the center pane in Image 6 exposes the controls for importing, editing, and creating costumes and backgrounds, as well as recording and/or importing sound files to be used asmusic and sound effects.

The Scratch programming language

Despite the fact that Scratch has amassed a huge following since its Beta release on March 4, 2007, in my opinion, the language isn't a particularly goodlanguage from a computer science viewpoint. Of the ten or fifteen programming concepts that most computer science professors consider to befundamental to good programming, Scratch supports only a few. Those few include:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask