Time for action – making the ground shake

The game file to import and load for this session is 5961_07_03.stencyl.

  1. Open the Manage Statues actor behavior and create a new When Creating event.
  2. Rename the new event to Initialize statue.
  3. Add the instruction block to the new event, as shown in the following screenshot:
    Time for action – making the ground shake
  4. Create a new When Updating event and rename it to Check if stopped.
  5. Add the required instruction blocks, so the new event looks like the following screenshot:
    Time for action – making the ground shake
  6. Create a new When this actor collides with… Something Else event and rename it to Shake screen on collision.
  7. Modify the new collision event using the instruction blocks shown in the following screenshot:
    Time for action – making the ground shake
  8. Test the game; watch as the statues land on any object!

What just happened?

We have implemented the instructions to make the screen shake when a statue lands, either on a tile, such as the ground, or on an actor.

The instructions for this effect comprise of three separate parts. The first part required us to specify an actor value for the statue when it is created; remember that actor values will apply uniquely to each statue actor as it is created. We called the actor value Stopped and set it to false, because the actor is moving the instant it enters the scene. In other words, it has not stopped!

We then implemented the second part of the instructions in the statue's when updating event, which we renamed to Check if stopped. This section checks the x and y speed of the statue and if both are less than 0.1, which effectively means that the statue is not moving, we set the actor value Stopped to be true.

Note

Why don't we test for an x and y speed of zero? Due to the design of the physics engine that Stencyl uses, waiting for an actor to reach a speed of absolute zero can take a while (and sometimes never happens!), causing the screen to shake excessively.

Try changing the Check if stopped event to test for a speed of zero, retest the game and wait for several statues to fall. Don't forget to change the values back once the test is complete!

Finally, we created the third part; a when this actor hits something else event, which checks the status of the actor value, Stopped, and if it is false, the shake screen… block is executed.

If we write the instructions for all three parts in English, the result will be as follows:

When a statue actor is created, give it an actor value called Stopped, and set it to false. While the game is running, if the statue stops moving, set the value of Stopped to true. While the game is running, if the statue collides with anything, and its Stopped value is false, shake the screen.

The implementation for the shaking effect might appear to be more complicated than required! Why do we need an actor value, and why do we need to check whether or not the statue has stopped moving?

The answer lies in how the collision detection works in Stencyl. When using the when this actor hits something else block or any other type of collision, a collision is occurring for the whole time when the two objects are touching. In the case of our statues, they might collide with a platform causing the screen to shake once, but then the statue might fall onto the tiles that represent the ground, and just lie there, apparently motionless. However, while the statue is lying on those tiles, a collision is still occurring, so the screen will shake continuously until the statue disappears. Such an effect will cause an eye-popping experience for players of our game, because there is always at least one statue onscreen at any time!

If we cast our minds back to Chapter 6, Managing and Displaying Information, when we implemented the instructions for counting the collected fruit in the section entitled Counting collected actors, we used an actor value as a Boolean flag to determine whether or not a fruit actor had been collected. This enabled us to ensure that each fruit actor was only collected once, thus avoiding an incorrect count. The use of the actor value, Stopped, in our new screen-shaking instructions follows the same principle. We are using the actor value as a flag to determine whether or not we still want the statue to cause the shaking effect.

To see what happens if we do not test the value of the Stopped actor value, we can carry out a basic test.

Temporarily change the Shake screen on collision event in the Manage Statues behavior, so it looks like the following blocks:

What just happened?

Now, if we test the game, we can clearly see the problem. The screen starts to shake as soon as a statue hits any object and it rarely stops shaking because there is almost always at least one statue colliding with another object!

Don't forget to revert to the correct code! If required, review the previous section Time for action – making the ground shake, step 7, to ensure that the correct instruction blocks are in place.

The instructions for making the screen shake work well, but there is definitely room for improvement. For example, test the game and when a statue lands on a platform and stops moving, make the monkey push the statue off the platform so the statue lands on the ground. It may take some time for a statue to fall in the right place to carry out this experiment but if we are patient, we will see that the screen does not shake when the statue collides with any other objects after being pushed by the monkey. This is because, after the statue lands on a platform and stops moving, the Stopped actor value is immediately set to false; the statue can never shake again!

Have a go hero

The game file to import and load for this session is 5961_07_04.stencyl.

For our purposes, we may be happy with the way that the screen shaking is experienced by the user. However it will be an interesting challenge to improve the current effect:

  • Consider how it might be possible to ensure that the statue does not shake the screen when it is stationary, but also reactivates its shaking ability if it is pushed or falls off a platform.

Please be aware that this is quite a difficult challenge, and there are numerous solutions! Be sure to save the game, and perhaps give the saved game a new name (File | Save Game As…), before attempting to modify the events. If things go terribly wrong (and sometimes things do go terribly wrong when experimenting), it's much easier to load an earlier version of the game, than it is to recreate the behavior!

Note

As noted, this is a comparatively a difficult challenge; the game will work perfectly well as it is. There's no harm in skipping this challenge and returning to it at a later time if desired.

A suggested solution is provided in the download file 5961_07_05.stencyl, along with comments in the Check if stopped and Shake screen on collision events of the Manage Statues actor behavior.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.20.133