How to Get Number of Costumes of a Sprite

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

Шаблон:Wiki Standards Шаблон:Expand It is possible to find how many costume numbers a sprite has in Scratch. This is a tutorial on how to find how much costumes a sprite has.

This can be used to debug sprites.

Setup

One method is to use the following script:

whenclickedswitchcostumeto0*1ifoneusesthejoinblockwith0andnothing,ifacostumeiscalled"0"itwillnotworkset#ofcostumestocostumenumberitisalocalvariableswitchcostumetocostume1Startcostume.

Then, run the project and the variable should report how many costumes a sprite has.

This method works because the switchcostumeto block uses the formula:

input-1-floorofinput-1/#costumes*#costumes+1

<ref>https://github.com/LLK/scratch-vm/blob/develop/src/blocks/scratch3_looks.js#L392</ref><ref>https://github.com/LLK/scratch-vm/blob/develop/src/sprites/rendered-target.js#L438</ref><ref>https://github.com/LLK/scratch-vm/blob/develop/src/util/math-util.js#L42</ref><ref>https://github.com/LLK/scratch-vm/blob/develop/src/blocks/scratch3_looks.js#L605</ref>

to determine which Шаблон:Val to select, which—with 0 as the input—evaluates to:

0-1-floorof0-1/#costumes*#costumes+1
-1-floorof-1/#costumes*#costumes+1
-1--1*#costumes+1
-1+#costumes+1
#costumes

One can also do:

whenclickedsetnumberto0switchcostumetocostume1nextcostumerepeatuntilcostumenumber=1nextcostumechangenumberby1

Even though the previous way is easier and faster, this also works.

References

<references />