Clone functions are used widely when we design Scratch projects. We could use “create clone” to create multiple sprite clones with similar appearance or behaviors. For example, in a star war game, we could use clones to create multiple enemy missiles. In a fish eating game, we could use clones to create a school of fish which could swim toward various directions.
In this post, I will introduce some typical usage of clones, issues and limitations. The sample project I use comes from one lesson of The Coding Fun courses. It depicts a little boy entering into a magic forest. Magic fruits are dropping down from the sky continuously. If he gets them, he will grow bigger and stronger.
The original project could be found at: https://scratch.mit.edu/projects/413365538/
Typical Usage of Clone
In this sample project, fruits sprite creates clones at a regular internal. The typical way of using clone is to wrap “create clone of myself” in a forever block and then hide the sprite itself. In the event receiver of “when I start as a clone”, it sets clone’s position, costume, and movement. It will also use conditional judgement to decide if the clone should be deleted. As shown below.
The clones could have different positions and costumes easily just by using “pick random” block. However, the majority of clones’ behavior is the same.
Use Local Variable
If we want the cloned sprites carry more specific behaviors, we could use local variable. When we create a new variable, we could choose from two types: For all sprites or For this sprite only.
Literally, if we choose “For all sprites”, this variable is visible for all the sprites and any sprite (including backdrop) could get or set the value of this variable. If we choose “For this sprite only”, only the sprite which creates this variable could “see”, set or get value for it. When we choose other sprites, this variable will not appear in the Variable category, so it could not be set or get.
Besides the above difference, when a variable is local (choose “For this sprite only”) and is set a value before “creating clone of myself”, this local variable will get a copy for each cloned sprite. In this way, we could assign a unique ID for each clone, as shown in the below updated code sample.
We could use this local variable in “when I start as a clone” code segment to better control the behavior of each clone.
Scratch project could support existence of multiple clones simultaneously. However, there is a upper limit to it. If I change the above example by removing the clone deletion part in “when I start as a clone” code segment, all the clones will keep existing. We will notice that when there are more than 300 clones in total, new clones are still creating but they will not show up on the stage.
The effect would be like what this video shows. Another phenomenon you would notice is that when the clones touch the edge but get not deleted, they will be “stuck” on the stage edge, looking a bit weird. Therefore, unused clones should better be deleted in time.
This clone limitation issue is also presented in the Scratch project Pixel Silhouette Scanner. You could check the post to see how I remix the project and workaround this limitation.
The typical place to delete clone is in “when I start a clone” code segment. when a sprite clone meets a certain condition, the single clone could be deleted, but other clones will not get affected.
If you want to delete all of the clones , just use “delete this clone” in other places. For example, if you use “delete this clone” in the event receiver of “when I receive Success”, all the clones will be deleted.
That is all for the tips and tricks of using clone. keep tuned for our future update and don’t forget to enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.