Please note: This theme has not been updated to reflect Scratch 3.0. Visit this help article for more info.
Transcript

In this video you will add more objects to your project.

To add another object you could copy the code you created in the last video and change the new objects values in the switch costume, set y, and set distance variables.

That would be a lot of repeated code, which is confusing and hard to manage.

Computer scientists love saving time by avoiding repeated code.

You can use a function to save time.

Functions let you name a big block of code.

The code needs to be created and named only once, but can be used in many places with different values.

Create a function called create objects.

To make different types of objects appear on the screen, add some parameters.

Add a parameter by clicking add input.

A string is a sequence of letters or characters.

Name in the input costume.

Add a number input called height and another number input called distance.

Click OK.

Program the function to set the costume, height, distance, and size, then create a clone.

Drag the block stack under the one flag clicked block that you created in the last video to the define create objects block.

Place each parameter in its own, proper place in the block stack.

Costumes to switch costume, height to set y, and distance to set distance.

To have multiple objects move across the stage, create a clone.

Add a create clone block to the bottom of the code stack.

When a clone is created in your program it uses the same variable name in its code as the other clones, or the original sprite.

However, the values for the variables are clearly different for each clone.

You've previously seen that you can set a different distance and height for each new object.

When you set a variable as for this sprite only, Scratch makes a copy of that variable for every clone it creates.

Make the clone move with the left and right arrows.

Replace the one flag clicked event with a when I start as a clone block.

Now that you've created the function, you can use it to make object clones with different costumes, heights, and distances.

Add a create objects block under the one flag clicked event.

In the first value of the block input the name of the costume you want to display.

In the second value input the height at which you'd like the object to appear on the stage.

In the third value input the distance from the walking sprite.

Distances between one and 10 work best.

Click the flag, then press the arrow keys to test.

Oh no, there's an extra object.

Tell the program to show only the clone of the object sprite and not the object sprite itself.

Above the create objects block add a show block.

Below the create objects block add a hide block.

Click the flag and press the arrow keys to test.

Awesome, the original sprite hides and the clone shows.

To create clones that use a different costume add another create objects block.

Click the costumes tab again and decide which other costumes you'd like to use.

Input the costume name in the first value of the new create objects block.

Set the height and distance and click the flag to test.

Tinker with the height until it looks good to you.

Next, add more costumes to your project if you'd like.

Click the costumes tab and see if there are any other costumes you would like to include or draw your own costume.

Add another create objects block.

This example adds a create objects block that sets the costume to the mountains at the bottom of the code stack.

Click the flag to test.

Oh no, the mountains appear in front of the trees, but the mountains should appear behind everything else.

If your objects layer in the wrong order try reordering the create objects blocks.

If one of your costumes is not showing up, check the spelling of the costume name.

If the name is not spelled exactly right, with the correct capitalization, the right costume will not show up.

Finally, add a story to your animation.

Click the walking sprite.

Add a say block and type in a message.

To start the code, add a one flag clicked block.

Add more say blocks to your projects to tell a longer story.

Now it's your turn.

Make a create objects function and add a parameter that sets the sprite's costume.

Add parameters to set the sprite's height and distance.

Replace the one flag clicked event with a when I start as a clone block.

And create objects blocks and set the costume, height, and distance in each.

Optionally, add say blocks to tell a story.

Instructions

  1. Make a "create objects" function, and add a string parameter that sets the sprite's costume.
  2. Add number parameters to set the sprite's height and distance.
  3. Replace the "when flag clicked" event with a "when I start as a clone" block.
  4. Add two "create objects" blocks, and set the costume, height, and distance in each.
  5. Optionally, add more "create objects" blocks, and add "say" blocks to tell a story.