Cloning is not easy to use in Scratch
In Scratch games, we often use clones, especially in games that require a lot of enemy characters. Using clones can help us avoid creating many duplicate sprites. However, cloning is not very easy to use, especially when you need to distinguish between multiple clones of the same sprite. Some experienced Scratchers will use variables that only apply to the current sprite to solve the problem of identifying clones. For example, using variables that only apply to the current character to give clones an ID. This process is often more complex and less intuitive.
In Gandi IDE, we have developed the Dolly extension to solve various problems when using clones.
In the Showcase game Dizziness, out-of-control robots randomly appear from the left and right sides of the stage, approach and attack the player. This is where we use the blocks in Dolly to generate clones of the out-of-control robot.
This is the project file for this best practice. Let's take a look at how it's done.
Prerequisites
You need to have a basic understanding of cloning in Scratch. You can refer to this official Scratch Wiki to learn more.
Before using Dolly, you need to install the Dolly extension in the extension library of Gandi IDE.
For detailed instructions on how to use Dolly, please refer to this document.
Step-by-step Guide
Step 1: Pre-setting for sprite
The costume of the enemy character is shown in the figure. It also has a series of frame animation costumes.
In the game, two types of clones need to be generated, one facing right and the other facing left. Therefore, the rotation style of the sprite is set to left-right flip to facilitate the generation of clones with the correct appearance.
Step 2: Generate clones with preset direction property
Use this block in the Dolly extension to generate clones.
In the block, you can preset a property for the clone. After the clone is generated, you can use another block to get this property.
For example, we preset the direction property for the clone. Fill in "direction" in the key field and "left" in the value field to indicate that this clone is a clone facing left.
Similarly, if you want to generate clones facing right, you can fill in "right" in the value field.
Step 3: Set the position and direction of clones according to property
You can use this block in Dolly to get the property of the clone.
When the clone is generated, according to its direction property, let it face the right direction, move to the appropriate position. The clone facing right needs to be moved to the far left, and conversely, the clone facing left needs to be moved to the far right. Then, the clone needs to move forward and change its costume to play the frame animation.
Step 4: Conditions for the appearance of clones
After the program starts running, enemies appear randomly. The longer the game time, the more frequent the appearance of enemies. We can use random numbers and timer to make it. The script is like this.
In this way, we can generate clones moving in different directions.
Note
Cancel the limit on the number of clones
In Scratch, the max number of clones is 300. In Gandi IDE, you can use this block in the Fermi extension to set the max number of clones.
However, it should be noted that too many clones may cause the program to use too many resources and cause lag, so use it with caution.
In addition, you can also use this block in Fermi to cancel the fence of the stage area, so that clones can move outside the stage area.
For detailed instructions on how to use the Fermi extension, please refer to the documentation.