Falling Stars

Super Power-Ups!

Now that you have a new power-up collectable working, it’s time to make it do something really cool: Let’s make it ‘rain’ power-ups for a few seconds, instead of just giving out an extra life.

For this, you’re going to use another broadcast message.

First, change the react-to-player block to broadcast a message when the player character touches a type 2 collectable. Call the message collectable-rain.

definereact-to-playertypeiftype=1thenchangepointsbycollectable-valueiftype=2thenchangelivesby1broadcastcollectable-rain

Now you need to create a new piece of code inside the Collectable sprite scripts that will start whenever the collectable-rain message is broadcast.

Add this code for the Collectable sprite to make it listen out for the collectable-rain broadcast.

whenIreceivecollectable-rainsetcollectable-frequencyto0.000001wait1secondssetcollectable-frequencyto1

What does the new code do?

This piece of code waits to receive a broadcast, and responds by setting the collectable-frequency variable to a very small number, then waiting for one second, and then changing the variable back to 1.

Let’s look at how the collectable-frequency variable is used to find out why this makes it rain collectables.

In the main game loop, the part of the code that makes Collectable sprite clones gets told by the collectable-frequency variable how long to wait between making one clone and the next:

repeatuntilnotcreate-collectables=trueif50=pickrandom1to50thensetcollectable-typeto2elsesetcollectable-typeto1waitcollectable-frequencysecondsgotox:pickrandom-240to240y:179createcloneofmyself

You can see that the wait block here pauses the code for the length of time set by collectable-frequency.

If the value of collectable-frequency is 0.000001, the wait block only pauses for one millionth of a second, meaning that the repeat until loop will run many more times than normal. As a result, the code is going to create a lot more power-ups than it normally would, until collectable-frequency is changed back 1.

Can you think of any problems that might cause? There’ll be a lot more power-ups…what if you kept catching them?