Time for action – introducing randomness to our behavior

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

We're going to modify our instructions so that the statues are dropped at a random location, so ensure that the Drop Actors Randomly behavior is being displayed. Execute the following steps in order to introduce randomness to our behavior:

  1. Click and drag the green multiplication block out of the addition block, and drop it onto the gray background, below the orange do every 3 seconds block, as shown in the following screenshot:
    Time for action – introducing randomness to our behavior
  2. Click on the drop-down arrow in the now empty textbox in the green x of camera+ … block.
  3. Select Math from the drop-down menu and then, in the Operations section of the pop-up menu, click on the random number between … block.
  4. Enter the number 0 into the textbox immediately after the word between.
  5. Click on the drop-down arrow in the textbox immediately after the word and.
  6. From the drop-down menu, select Scene, and then click on the screen width block:
    Time for action – introducing randomness to our behavior
  7. Press Ctrl + K or, on Mac OS X press Command + K.
  8. Test the game. Make the monkey run left and right, and watch as several statues fall from the sky.

What just happened?

The actors are now falling at a random location somewhere between the leftmost edge of the screen and the rightmost edge of the screen.

The first step was to drag the screen width x 0.5 blocks out of the behavior because we no longer needed them. We then added the random number between … and … block, which allows us to specify a low number and high number, and then uses this information to generate a random number within that range. We specified a lower number of 0, which is the x coordinate for the left-hand side of the screen, and a higher number of screen width, which is the x coordinate for right-hand side of the screen. When our game is running, the random number between 0 and screen width instruction will automatically generate for us a random x position between zero and 640. The random number is then added to the x of camera to ensure that the statues always fall within the viewport.

Finally, we pressed the key combination of Ctrl + K, or Command + K on Mac OS X. This shortcut removes any unused blocks from the behavior designer, such as the ones that we dragged out of the behavior in the first step.

Unused blocks don't have to be removed from the behavior designer, they are ignored when the game is compiled, and they don't use any extra memory in our compiled game. However, it's a good idea to tidy up the behavior editor when the blocks are no longer required.

Optimizing the number of actors

Let's solve two more problems. The first problem is a technical one; depending on the speed of the computer's processor, after approximately 10 to 20 statues have been dropped, the game starts to lag, it slows down to an unacceptable speed because there are too many active actors on screen. Computers have limited resources and it is our job, as the game developer, to use these resources effectively.

The second problem is a gameplay issue; if too many statues have fallen, it can become impossible to complete the level, because the statues might block the monkey's path through the jungle. In some games, this may be the desired gameplay, but we want to create a game that is a little less stressful for our player on the first level, perhaps gameplay could become more difficult in later levels, but not just yet.

There are many ways to resolve such problems in a game, and it's usually down to the imagination of the developer to find a solution. We're going to solve both problems simultaneously, with a new behavior.

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

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