Knowledgebase / Use a time delay block to animate a game object
With the script editor you can use blocks that create time delays to prevent the other blocks from running until a certain amount of time has passed. In this tutorial we are going to use them to make an explosion that expands, rotates, and fades away.
Animating the explosion
Here’s the finished script that we’ll be creating:
Since we want the explosion to change bit by bit over time, we’ll use a function, which allows us to run a bunch of blocks repeatedly. We’ll start with making the function (which is basically a container for blocks that we can trigger whenever we want). To create a function, open the Functions tab and drag out the “to do something” block.
Change the name from “do something” to “animate”, or “expand, rotate, fade”.
Then grab the “set scale x”, “set scale y”, “set rotation” (all from Physics) and “set alpha” (from Looks). These blocks will change how large the game object is, how it rotates, and how transparent it is. Drag these blocks into the function you’ve just created.
We want the scale, rotation, and alpha to change a little bit each time this function runs. Here’s how we want those to work:
set scale x of myself to: scale x of myself + 0.1
set scale y of myself to: scale y of myself + 0.1
set rotation of myself to: rotation of myself + 5
set alpha of myself to: alpha of myself – 0.02
We’ll need to grab some addition and subtraction blocks and the blocks that get us the value of scale x, scale y, rotation, and alpha.
You can tweak these numbers to make those values change more quickly or more slowly.
The next block we need is the time delay block. It reads like this: “100 milliseconds have passed”. Grab this block from the Control tab.
Now what we want to happen is for this function to make these blocks trigger themselves over and over again. To do this, we’ll grab the “run function” block from the Functions tab and drag it inside the time delay block.
Now whenever this function runs it will make itself run again and again each time 100 milliseconds passes. The final blocks to add will be the initial event that gets the whole process running. In this example we’ve used the “When I am touched, get toucher” block, as well as the “if” block to check if the toucher has the tag “player” – this means when the player touches the explosion, it will trigger the function we’ve created.
That’s all there is to it! Try to think of other ways you can use functions and time delay blocks to animate different parts of your game.