From Test-Scratch-Wiki
The usage of 分身 has effect of multiple 角色 when only one sprite is used. There are two types of clones: those that need to share values and therefore affect other aspects of a 专案, and those that do not.
Choosing the Type
If a clone needs to have it's data (any info about them, such as hit points, x 座标, y 座标, etc.) accessible to the other clones or sprites, an affecting clone should be used. If a clone's data does not need to be accessed by anything but itself, a non-affecting clone should be used.
Making Non-Affecting Clones
This type of clone does not require shared information, and therefore is simpler.
Making the clones based on order of 作品
One way make non-affecting clones is to give each clone an identification number based on which order they were created in.
Preparation
First you will need the following 变量:
- A local variable to tell the clones what ID they have, called "buttonNumber" in this tutorial
- Local variables for whatever properties the clones to have, such as velocity or HP
编写程式
Now, make the following scripts, inside the sprite you are making these clones from:
當 @greenflag 被點擊 變數 [buttonNumber v] 設為 [0] 重複 (3) 次 變數 [buttonNumber v] 改變 (1) 分身 [自己 v] 建立 end // replace with the number of clones you need
Then, you can start scripting for each button or thing individually:
當分身產生 如果 <(buttonNumber) = [1]> 那麼 . . . end 如果 <(buttonNumber) = [2]> 那麼 . . . end 如果 <(buttonNumber) = [3]> 那麼 . . . end
If there's something they should all do instantaneously, put a command block before the if statements. Even better, add another When I Start as a Clone hat block and put the command(s) there.
Making the clones based on many attributes
Sometimes, however, in cases such as making a turret, there will be too many combinations of 方向, 造型, and other attributes to count. In this case you should break up the types into many attributes.
Preparation
For this, you will do the same thing, but make many global variables for different outcomes. In this example, the following will be used:
- "bulletType" for distinction between the player's bullets and the opponent's
- "shotX" and "shotY" for the starting position of the bullet
- "shotDirection" for the direction the bullet will move in
编写程式
Whenever you want a character to shoot, put this script after the trigger (like after the space bar is pressed, or some kind of condition):
當 [空白 v] 鍵被按下 // replace with whatever the trigger is 變數 [bulletType v] 設為 [goodguy] 變數 [shotX v] 設為 (x 座標) 變數 [shotY v] 設為 (y 座標) 變數 [shotDirection v] 設為 (方向) 分身 [bullet v] 建立 // The name of the sprite you are making the clones from
Now, make these scripts in the bullet sprite:
當分身產生 定位到 x: (shotX) y: (shotY) 面朝 (shotDirection) 度 如果 <(bulletType) = [goodguy]> 那麼 造型換成 [goodShot v] 重複直到 <<碰到 [邊緣 v] ?> 或 <碰到 [enemy v] ?>> 移動 (10) 點 end end 如果 <(bulletType) = [badguy]> 那麼 造型換成 [badShot v] 重複直到 <<碰到 [邊緣 v] ?> 或 <碰到 [player v] ?>> 移動 (10) 點 end end
"Effects" With Non-Affecting Clones
Effects can be a number of things:
- Clouds, fog, birds, and other ambiance objects.
- Things that add realism, and in fact are particles, such as smoke from a gun, fire from an engine, and the like.
To make these, just use the previously prescribed method, and have as many types of effects as you need, such as this project. Effects such as these actually would use the same variables as bullets, as they must be told where to go.
Making Affecting Clones
These clones are as mentioned before, the type that other sprites or clones need their data, such as their position. They generally follow the same method, but store info in lists so other sprites can access it by ID.
Preparation
To make these type of clones, you will need the following:
- A global variable called "cloneNum" or something. This serves as a counter variable.
- A local variable called "myID", so the clones can identify themselves.
- Global lists for shared properties. In this example the lists "Xposition" and "Yposition" will be used to track the coordinate locations of the clones.
编写程式
Put these scripts into the sprite you want to have cloned:
當 @greenflag 被點擊 變數 [myID v] 設為 [0] 刪除第 (全部 v) 項 \( [Xposition v] \) 刪除第 (全部 v) 項 \( [Yposition v] \) 重複 (10) 次 新增項目 [] \( [Xposition v] \) 新增項目 [] \( [Yposition v] \) 變數 [myID v] 改變 (1) 分身 [自己 v] 建立 end // This is how many clones you want.
For the clones, put this:
當分身產生 重複無限次 替換第 (myID) 項於 [Xposition v] 成 (x 座標) 替換第 (myID) 項於 [Yposition v] 成 (y 座標) end
You may also combine the two methods and make different classes of clones while also sharing data.
用法示例
Some things this could be used to make are:
- Buttons from one sprite
- A lot of enemies for a game from one sprite, including different types of enemies
- Complex or semi-complex particle systems.
- A sprite called "effects" and have all effects, such as dust clouds, explosions, and other effects all come from this sprite.
- A Tower Defense game, having as much as 200 or so towers on a map.
- A sprite for bullets, have as much of those as you want, for as many different characters as you need.