Advanced Clone Usage

Материал из Поле цифровой дидактики

The usage of clones has the effect of multiple sprites 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 project, and those that do not.

Choosing the Type

If a clone needs to have its data (any info about it, such as health points (HP), X position, Y position, 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 creation

One way to 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 variables:

  • 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

Scripting

Now, make the following scripts, inside the sprite you are making these clones from:

when gf clicked
set [buttonNumber v] to [0]
repeat (3) // replace with the number of clones you need
change [buttonNumber v] by (1)
create clone of [myself v]
end


Then, you can start scripting for each button or thing individually:

when I start as a clone
if < (buttonNumber) = [1] > then
. . .
end
if < (buttonNumber) = [2] > then
. . .
end
if < (buttonNumber) = [3] > then
. . .
end

If there is something that 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 directions, costumes, 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

Scripting

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):

when [space v] key pressed // replace with whatever the trigger is
set [bulletType v] to [goodguy]
set [shotX v] to (x position)
set [shotY v] to (y position)
set [shotDirection v] to (direction)
create clone of [bullet v] // The name of the sprite you are making the clones from

Now, make these scripts in the bullet sprite:

when I start as a clone
go to x: (shotX) y: (shotY)
point in direction (shotDirection)
if < (bulletType) = [goodguy] > then
  switch costume to [goodShot v]
  repeat until <<touching [edge v]> or <touching [enemy v]>>
    move (10) steps
  end
end
if < (bulletType) = [badguy] > then
  switch costume to [badShot v]
  repeat until <<touching [edge v]> or <touching [player v]>>
    move (10) steps
  end
end

"Effects" With Non-Affecting Clones

Effects can be a number of things:

  • Clouds, fog, birds, and other ambience 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.

Scripting

Put these scripts into the sprite you want to have cloned:

when gf clicked
set [myID v] to [0]
delete (all v) of [Xposition v]
delete (all v) of [Yposition v]
repeat (10) // This is how many clones you want.
add [] to [Xposition v]
add [] to [Yposition v]
change [myID v] by (1)
create clone of [myself v]
end

For the clones, put this:

when I start as a clone
forever
replace item (myID) of [Xposition v] with (x position)
replace item (myID) of [Yposition v] with (y position)

You may also combine the two methods and make different classes of clones while also sharing data.

Example Uses

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.

See Also