From Test-Scratch-Wiki
With the introduction of 分身 in Scratch 2.0, 专案 have many efficient capabilities to perform tasks without creating an extensive number of 角色s. Clones are instances of sprites, meaning they inherit the properties of the sprite but are separate objects. Clones commonly may have a slightly different task to perform than the parent sprite, but one barrier is that both clones and sprites respond to almost all of the 事件类积木 (triggers). Therefore, a trigger specifically designed for a sprite will also be run by a clone when signaled.
The Concept
One way to understand the concept is with the following simple 程式:
當角色被點擊 定位到 x: (0) y: (隨機取數 (-100) 到 (100)) 重複 (10) 次 x 改變 (3) end
Contrary to its name, if a clone of the sprite containing the above script was to be clicked, the clone itself would perform the script. What if one only desires for the sprite and not its clones to respond to the script? One can customize which triggers are run by what class (i.e. parent sprite or child clone) through the simple use of a 局域变量.
Private 变量 store values for individual sprites and clones, although they have the same name. For example, three clones can have a variable named "x velocity", but each clone can have its own individual value of its variable. In a similar way, a sprite can have a variable set to a particular 字串 while every clone has it set to a different value. This concept of differing variable values between classes allows one to choose which scripts are run by the parent sprite or child clones.
Programming
When the 绿旗 is clicked, all clones are immediately deleted but the parent sprite still exists. This is the correct time to set the sprite's variable to an indication of its class (position on the hierarchy).
當 @greenflag 被點擊 變數 [instance v] 設為 [sprite] // "instance" must be a private variable
This small bit of data saved in the variable "instance" simply shows that the sprite is a sprite. "Instance" must be a Private Variable or else it will not work. Next, a script must be designated for assigning all clones a variable value that shows they are clones. The following script can accomplish this:
當分身產生 // a sprite cannot run this script, only clones 變數 [instance v] 設為 [clone]
The above script will not change the sprite's variable. Instead, each individual clone will have its own variable. Once this is done, the variable in conjunction with an if statement can properly designate triggers to only run for the sprite or its clones.
當收到訊息 [fadeAway v] // this script only works for clones 如果 <(instance) = [clone]> 那麼 重複 (10) 次 效果 [幻影 v] 改變 (10) end end
The above script only will work for the clones because the sprite's variable is not set to "clone", and the sprite will not execute the blocks inside the statement. This variable method is an efficient way for debugging projects in which clones are causing trouble.