In this program, the hat and the cat are two separate sprites to begin with. However, when the cat touches the hat, the hat hides and the cat switches to a different costume -- one with a hat.
When you need both characters to do something when they are touching and one needs to hide, it is best to have one character sense if they are touching and send a broadcast. The other character will use a When I receive broadcast to tell it what to do when they are touching. This is due to timing issues if you have one character hiding. An alternative is to have the character that hides actually use ghost. That way it looks hidden but the other characters can still sense it.