Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

4. Add More Objects

Transcript

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

To add another object, copy the code you created in the last video, and change the new object’s 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 “options,” then “add string input.”

A string is a sequence of letters or characters.

Name 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 "when flag clicked” block that you created in the last video to the "define ‘create objects’" block.

Place each parameter in its 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 block stack.

When a clone is created in your program, it uses the same variable names 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 "when flag clicked" event with a "when I start as a clone" block.

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

Add a "create objects" block under the "when 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 1 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.

The "create objects" blocks have to be in order, from the object that is farthest back to the one that is in front.

The clones are layered in the order of the blocks.

So, the object in the first "create objects" block appears behind all the objects created after it.

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

Click the scripts tab, and 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.

Click the scripts tab, and 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 "when 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 string parameter that sets the sprite's costume.

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

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

Add two "create objects" blocks, and set the costume, height, and distance in each.

Optionally, add more "create objects" blocks, and add "say" blocks to tell a story.

arrow_backward Back
Next arrow_forward
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.