Friday 8 March 2013

Scratch Programming - Layers

All sprites in Scratch have three coordinates: x, y and z.

The x and y can be set directly:
The z cannot be set directly but can be adjusted by changing the sprites layer. This is not as intuitive as it might be (well it certainly confused me for a while).

I imagined that a sprites layer number would work in a similar manner to the x and y coordinates. So a sprite which has been told to "go back 5 layers" would be behind one which has been told to "go back 3 layers".

The reality is that every sprite has a unique layer. When you tell a sprite to "go to front" it does just that. It takes a position in front of all other sprites (let's call this layer 1). The sprite that was on layer 1 moves back to layer 2 and so on down the line.

When you tell a sprite to "go back 1 layers", it basically swaps places with the sprite that was behind it.

When you tell a sprite to "go back -1 layers" (you can use negative numbers) it swaps places with the sprite that was in front of it.

So basically all layers are relative, not absolute.

I added the following code blocks to a sprite in a 2 sprite program:

When you press "a" the sprite goes behind the other sprite.

When you press "b" it comes back to the front again (you do not have to press "b" twenty times!!)