Time for action – making the behavior more flexible

The game file that needs to be imported and loaded for this session is 5961_06_07.stencyl.

Ensure that the Drop Actors Randomly behavior is being displayed. Execute the following steps in order to make the behavior more flexible

  1. Delete the number 320 from the right-hand textbox in the green addition block (it's not strictly necessary to delete this, but it keeps everything neater).
  2. Click on the drop-down arrow in the same textbox from which we just deleted the number.
  3. In the Math section of the pop-up menu, click on the 0 x 0 block.
  4. Click on the drop-down arrow in the left-hand textbox of the newly added green multiplication block.
  5. Select Scene from the drop-down menu and then, in the Basics section of the next pop-up menu, click on the red screen width block.
  6. In the right-hand textbox of the green multiplication block enter the number 0.5. Your screen should like the following screenshot:
    Time for action – making the behavior more flexible
  7. Test the game.

What just happened?

Nothing appears to have changed in the game because the statues are still dropping in exactly the same location. However, we have ensured that, whatever the width of our screen, the statue will always drop in the center of the screen rather than at the fixed location that we originally entered. We have calculated the x position of the statue by adding half the width of the screen; that is, screen width x 0.5, to x of camera.

This may seem a trivial change to make, but there is another benefit. Imagine that we had 20 different behaviors that referred to the width of the screen, and that the numbers had been manually typed, as they were in our original version of the behavior. If we now imagine that we have decided to change the screen width in the Game Settings dialog box, then we'd have to work our way through all 20 behaviors and manually adjust the numbers! This would be a very time-consuming process and, even worse, it would increase the risk of introducing errors into our game.

Why did we multiply the screen width by 0.5, when we could have divided the screen width by 2, in order to achieve the same result? In theory, computer code can often multiply faster than it can divide, so some developers consider it to be a more optimized calculation. However, in practice, it's unlikely to have any effect on a game developed with Stencyl. The choice of whether to multiply or divide is entirely down to the developer.

Note

There is a potential disadvantage to calculating the x coordinate for the center of the screen, rather than manually typing the number into the instruction block. The computer has to calculate the x coordinate based on the width of the screen every time a statue is dropped, which uses processing power. This is unlikely to cause a problem for us, but it does demonstrate that game designers have a lot of issues to consider when building a game!

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

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