Step 5: Using "if-statements" to check collisions

We'll make it so the character only crashes if it hits one of the spiky obstacles. Edit the character script to start modifying it.

The next block we need will check if something is true. The key word there is IF. This is called an if statement -- this is very important for making things happen only if certain conditions are met. Grab the "if" block from Control. Drag it inside the "When I am touched get toucher" block, and drag out the "go to current level" block.

The other important part is "if WHAT?" The thing we are asking about has to either be true or false. If something is true, then we will make another thing happen. The thing we are checking to see whether it is true or not is called a boolean condition -- it is a piece of data that can either be true or false, on or off, yes or no. The boolean variable we will use is "Does this instance have this tag?" -- either the instance has the tag, or it doesn't.

We can find the "instance has tag" block from Sensing. It is named "myself has tag", but we are going to replace the myself part. Drag out the "myself has tag" block, and snap it to the side of the "if" block.

Then drag the "myself" block into the trash. We're going to replace it with another block named "instance toucher". The instance toucher block is a variable that has been created by the "When I am touched get toucher" block. Each time the character touches something, this block will tell us what instance we touched, and set that instance as a new variable so that we can remember it. That's the whole point of any variable -- to remember a piece of data.

To find this "instance toucher" variable block, open the Variables tab in the script editor sidebar. As soon as we used the "When I am touched get toucher" block, the "instance toucher" block was created in here. Drag it out into the empty gap left by the myself block.

Now we need to complete this if statement by changing the tag to killer. Once you've done that, the blocks should say "When I am touched, get toucher, if instance toucher has tag killer, do ..."

Lastly, we need to tell it what to do. We want it to make the player lose! So grab that "go to current level" block and drag it inside the "if" block.

Play your game to test it out! Now you should only crash if you touch the spike obstacles.

results matching ""

    No results matching ""