Cloning a Sprite
-
Introduction
In some projects, we need to use many objects that act similarly. For example, we might want to have 10 dogs walking around the stage randomly. One naive way to do it is to create 10 dog sprites, and add code blocks to each of them. The problem is that most of the code blocks are similar, so we may have to repeatedly add the same blocks 10 times.
A better way is to use “cloning”. When we clone a sprite, we make a copy of that sprite, including its code blocks, costumes and sounds. However, a key difference is that we only make the copy after the program has started running. So any change we make to a sprite’s code or costumes will be correctly copied after the program has started. When a program stops, all clones are deleted automatically.
All blocks related to cloning are located in the 'Control" category:
How to Create a Clone
To create a clone, you can use this block:
-
Sprite List: The first parameter specifies which sprite to clone. “myself” means the current sprite where this block is running. If there are other sprites in the project, such as “Basketball” or “Baseball”, their names will show up in this list as well.
-
Clone ID: The second parameter allows you to assign any number or text to that clone. If you leave it empty, a random and unique ID will be assigned to the new clone
For example, this program will create 3 clones of the current sprite, with IDs of “1”, “2”, and “3”.
Note that you will not be able to tell there are 3 clones, since they are overlapping with the original sprite. They all have exactly the same position, size, and costume as the original sprite.
Code to Run after a Clone is Created
To make the clone do something after it’s born, you can add new code blocks below this block:
For example, this code will simply move each clone to move to a random position, so we can clearly see all of them. Note that the original clone will not be affected by what the clones do.
Clone ID
Recall that when each clone is created, it is assigned a unique ID. Each clone can find out its own ID using this block.
For example, we can make the clones report their “clone ID”:
Ask a Clone to Delete Itself
A clone can delete itself after it has done its work, using this block:
Note that you can not attach any block below this block, since the clone will be deleted already.
For example, we can ask each clone to say its “clone ID” for 2 seconds, then delete itself.
Get Information About a Clone
The sprite pane below the stage will only show information on the original sprite. However, the original sprite can still get information about the clones by their IDs using this block:
-
Property Name: The first dropdown contains the list of properties you can read about a clone, such as its position, direction and costume.
-
Sprite Name: The second dropdown allows you to specify which sprite or clone you want to know about.
For example, after creating 3 clones, you can get their “x position” from the original sprite:
Communicating with Clones Using Messages
Although you can add code blocks for clones under the “when I start as clone” block, these blocks will only run once. Another way to “talk” to clones is to broadcast messages. The benefit is that you can send messages at any time.
For example, in this program, we broadcast “message1” in the original sprite after all clones are created. There is also a block for receiving this message. Since all clones have the same code as the original sprite, they will all run this “when I receive message” stack.
Note that the original sprite also responds to the “message1”, and it also has a random “clone ID”, even though it is not a clone. A common practice is to only make the clones do work, and hide the original sprite. For example, we can check if the “clone ID” is within the range of IDs we assigned to the clones. If it is not, then we are in the original sprite’s code, so we can do nothing and hide the sprite.
-