Name: ___________________________________________ 129 Lesson 10 Spawning Copyright Goodheart-Willcox Co., Inc. May not be reproduced or posted to a publicly accessible website. Random Number Generator Random numbers are used throughout video games, from spawning enemies to changing environmental effects, determining which cards are drawn, rolling dice, varying the likelihood of hitting an enemy, and many more applications. The platforms are designed to be different with the hole in different locations. A random number generator will select a number that will be used to vary the random animation direction. This will make different platforms appear in the game. The gameplay will be new each time the game is played. The function random is a computer programming method that randomly selects a number from the set defined by the number specified in the parentheses. The first number available is always 0. In this case, there are four animation directions, so the computer needs to randomly choose from 0, 1, 2, and 3. 72. Open the Game Frame in the event editor. 73. In event line for the Always condition, right-click in the cell below the Counter_Random icon, and click Set Counter in the shortcut menu. The expression editor is displayed. 74. Enter random(4) in the expression editor, as shown in Figure 10-12. 75. Click the OK button to set the expression. Note: if an error message appears, check that you spelled random correctly, and try again. 76. Verify the programming matches this pseudocode: IF always, THEN set Counter_Random to a value from 0 to 3. Spawning Spawning is the creation of a new object. This game will spawn new platforms and hearts to keep the gameplay going. 77. Applying what you have learned, add a new condition. In the New Condition dialog box, right-click on the Timer icon, and click Every in the shortcut menu. The Every dialog box is displayed. 78. Change the time setting to 1.70 seconds. You will have to use both the Second(s) and the 1/100 text boxes to make that setting. 79. Click the OK button to apply the setting. 80. On the event line for every 1.70 seconds, right-click in the cell below the Create new objects icon, and click Create Object in the shortcut menu. Generates a random number from 0 to 3 Goodheart-Willcox Publisher Figure 10-12. The expression editor can be used to create code that generates a random number.
Previous Page Next Page