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 spritecount should be created. Then, on the stage, the following script should be added:

whenclickedsetspritecountto0broadcastcountspritesandwait...

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:

whenIreceivecountspriteschangespritecountby1Thistakesadvantageofthefactthatscriptsarenotrunatthesametime.setmynumbertospritecountSwapthesetwoblockstostartcountat0insteadof1

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

This works because of the execution order of broadcasts.

See Also