arrow_back Mystery Box (Part 2 of 2)
In this video, you’ll animate objects to fly out of the box.
Click the “box” sprite. Hide the box sprite at the end of the animation using a “hide” block from the “looks” menu. To create the objects that fly out of the box, make a clone of the sprite, and switch its costume to an object.
Open the “control” menu, and add a “create clone of myself” block right before the “hide” block. Then, add a “when I start as a clone” block. Open the “looks” menu, and add a “switch costume” block. Select the object you want to appear from the dropdown menu. This example uses the “cupcake” costume.
Use a “wait” block to slow down the animation.
Click the “when I receive” block stack to test this code so far.
Cool! An object comes out of the box. Click the stop sign to clear the clone from the stage.
Next, make the object glide away from the box. From the “motion” menu, add a “glide” block. To make the object glide to a random spot, add “pick random” blocks from the “operators” menu to both blanks in the “glide” block. Type “negative 240” and “240” into the x values and “negative 180” and “180” in the y values to make the object glide to a random spot anywhere on the stage.
Click the stack a few times to test it out. Cool! The cupcake goes to a different spot every time. To make many objects fly out of the box at once, put a “repeat” block around the “create clone” block. Change the value in the “repeat” block to the number of clones that should appear. This example uses 50. Click the stop sign to clear the stage, then click the block stack to test this out.
Whoah! Awesome! Add a “delete this clone” block after the “glide” block, so the objects don’t stay on the stage forever. Click the stop sign, then the “when I receive” block stack to make sure this works.
This effect looks great so far, but there are still some small tweaks that would make it look even better. Computer scientists like you always look for ways to improve their programs.
First, the objects appear behind the box. To make the objects appear in front of the box, add a “go to front” block from the “looks” menu right after the “when I start as a clone” block. Test this out.
Cool! The objects also disappear a bit too fast. Add a “wait” block before the “delete this clone” block to make them stick around for a little longer. Test this out.
That’s a neat effect. Lastly, the objects fly all over the stage.
It may look better if the objects only glide towards the bottom of the stage. Change the “180” in the “pick random” block to a smaller number, like negative 60.
Test this out by clicking the flag.
Great! Now the objects only appear at the bottom of the stage.
Feel free to make even more tweaks to customize your box. Remember to test often!
Now, it’s your turn! Make an object fly out of the box using the “create clone,” “when I start as a clone,” and “switch costume” blocks.
Make many objects fly all over the stage using “glide,” “pick random,” and “repeat” blocks.
Delete the clones after they animate using a “delete this clone” block.
Improve the look of the animation by adding a “go to front” and “wait” block, and by tinkering with the values in the “pick random” blocks.