by David J. Malan <>
Printer-Friendly Version of Tutorial

« Previous | Table of Contents | Next »

Oscartime's Trash Sprite

Oscartime's Trash sprite is designed to fall from a random location in the sky to the ground:

Once picked up by the player and dragged to Oscar's trash can, the sprite falls from a new location, taunting the player. The process repeats forever. Of course, each disposal of the sprite is worth a point!

Let's examine the first of this sprite's threads:

This thread is pretty complicated, so let's consider it parts. Needless to say, this thread begins executing when the user clicks Scratch's green flag. The thread's first statements essentially aim the sprite downward, place it (invisibly) at some random location atop the screen, and, after four seconds, reveal the sprite:

Let's now consider the first conditional construct in the thread's loop:

Essentially, the construct above asks:
  1. Is the player depressing the mouse button? (If so, the player is presumably clicking on something.)
  2. Is the mouse touching this sprite? (If so, it is presumably on this sprite that the player is clicking.)
  3. Has the player just clicked on this sprite? (If so, we need to enter drag mode. If not, the player's already in drag mode!)
  4. Is the player definitely not clicking on some other sprite already? (We want to make sure the player can only pick up one sprite at once, even if they're overlapping on the screen.)
If all four conditions are met, the sprite sets a global, Boolean variable (named good_click) to true, so that the program remembers that a sprite (and not some random location on the screen) has, in fact, been clicked on for dragging; it also sets a local variable (named my_click) to true, so that the sprite knows that it is it that is being dragged); and it broadcasts an event (named trash_click) to itself so that a separate thread can handle the actual dragging.

If not all four conditions are met and the sprite is not already being dragged, the loop's second conditional construct induces the sprite to "fall" downward by one step unless it already appears to be lying on the ground:

The loop's final conditional construct determines whether the sprite has been dropped close enough to Oscar's trash can to be considered deposited (and thus worthy of a point):

Essentially, the construct above asks:
  1. Is the game still in progress? (Once Oscar finishes his song, after all, we don't want to accept any more trash.)
  2. Is the sprite within 20 pixels of Oscar's trash can? (If so, that's close enough to be considered deposited.)
  3. Is the player not depressing the mouse button? (Trash ain't deposited until you let go!)
If all three conditions are met, the sprite hides itself (as though it's been deposited), broadcasts an event (named scored) to one of the Oscar sprite's threads, waits a couple of seconds, moves to a new location in the sky, and reveals so as to begin a new descent.

Because all three of these conditional constructs are nested within a block labeled "forever," the sprite behaves as it should until game's end.

The aesthetics of dragging, meanwhile, are handled by a second thread:

Essentially, so long as, upon clicking the sprite, the player keeps the mouse button depressed, the sprite will follow the mouse's movements, thus creating the appearance of being dragged. As soon as the player releases the mouse button, the thread notes that neither this sprite nor any other is, for the moment, being clicked on anymore. The thread then "dies," to be re-"spawned" only when the trash is picked up again.

Oscartime's Sneaker, Newspaper, Clock, Telephone, Umbrella, and Trombone sprites essentially behave just like this Trash sprite, the only differences being the times at which they first appear. (Their appearance is synchronized with Oscar's first mention of them in his song.)

So let's conclude by looking at the last of Oscartime's sprites, Oscartime's Oscar sprite.

« Previous | Table of Contents | Next »