With the introduction of cloning in Scratch 2.0, projects have many efficient capabilities to perform tasks without creating an extensive number of sprites. 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 event blocks (triggers). Therefore, a trigger specifically designed for a sprite will also be run by a clone when signaled.
One way to understand the concept is with the following simple script:
when this sprite clicked go to x:(0) y:(pick random (-100) to (100)) repeat (10) change x by (3)
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 variable.
Private variables 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 string 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.
When the green flag 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).
when gf clicked set [instance v] to [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:
when I start as a clone //a sprite cannot run this script, only clones set [instance v] to [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.
when I receive [fadeAway v] //this script only works for clones if <(instance) = [clone]> repeat (10) change [ghost v] effect by (10) 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.