Counting Sprites

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

If you have a lot of clones with a local variable, they may want to count them automatically, rather than individually assigning each of them a number in a set () to () block. With this method, a script could specifically call the number-seven clone without hard-coding it.

Method

Firstly, a global variable (sprite count) should be created. Then, on the stage, the following script should be added:

when gf clicked
set [sprite count v] to [0]
broadcast (count sprites v) and wait
. . .

Now, for all the sprites that want to be counted (and generally these will be clones of the same sprite), this script should be added:

when I receive [count sprites v]
change [sprite count v] by (1) // This takes advantage of the fact that scripts are not run at the same time.
set [my number v] to (sprite count) // Swap these two blocks to start count at 0 instead of 1

Then, when the flag is clicked, each sprite will obtain a unique number in (my number).

This works because of the execution order of broadcasts.

See Also