There are several methods to find the costume count of a sprite in Scratch.
0th Costume Method
The switch costume to ( v) block accepts both costume names and costume numbers.[1] In the case of costume numbers, the number 0 wraps to the last costume, -1 is the second last costume, and so on.[2]
The number of costumes in a sprite is equal to the costume number of the last costume, leading to the following implementation:
when green flag clicked switch costume to ((0) + (0)) set [number of costumes v] to (costume [number v])
Code explanation:
- The script starts with a When Green Flag Clicked block as it only needs to run once, when the project is started.
- The switch costume block needs to be switched to number 0, so a reporter such as addition can be used to input a 0 into the block. A Join block will not work as it returns a string.
- The costume number is then stored in a variable for later use.
Loop Method
This method involves looping over every costume until the first costume is found. Each iteration increases a counter. This method is less efficient and more complicated than the first so use is not recommended.
when green flag clicked switch costume to (costume1 v) // switch to the first costume set [number of costumes v] to [1] next costume repeat until <(costume [number v]) = [1]> // repeat until the first costume is reached again next costume change [number of costumes v] by (1) end