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:

when gf clicked
switch costume to ((0) * (1)) // if one uses the join block with 0 and nothing, if a costume is called "0" it will not work
set [# of costumes v] to (costume [number v]) // it is a local variable
switch costume to (costume1 v) // Start costume.

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

This method works because the switch costume to ( v) block uses the formula:

(((input) - (1)) - (([floor v] of (((input) - (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)) - (([floor v] of (((0) - (1)) / (# costumes))) * (# costumes))) + (1)
((-1) - (([floor v] of ((-1) / (# costumes))) * (# costumes))) + (1)
((-1) - ((-1) * (# costumes))) + (1)
((-1) + (# costumes)) + (1)
(# costumes)

One can also do:

when green flag clicked
set [number v] to (0)
switch costume to (costume1 v)
next costume
repeat until <(costume [number v]) = (1)>
next costume
change [number v] by (1)

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

References

<references />