bbb Home |
Lesson 8 - Randomly Move Target |
SUMMARYIn this lesson you will learn how to use the Math object to move the target to a random location. In later lessons we will add a timer so that the target moves periodically. DISCUSSIONThere are many objects in JavaScript. These objects have functions that do special tasks. For your information, object functions are generally called methods. The Math object has methods that do mathematical tasks. For example, the random method creates a random number between 0 and 1. Referring to the example below, the function ranNumber() contains three statements on lines 5 to 7. On line 5, the Math.random() method is called. This function returns a number between 0 and 1. The number is then displayed in an alert. On line 6, Math.random() returns a number between 0 and 1. This number is multiplied by 200 and the result is displayed on the face of the button. When you run the program, you will notice that the result is a number between 0 and 200. On line 7, the button is positioned so that its left side is located between 0 and 300 pixels from the left side of the screen. On line 14, the function ranNumber() is called when the user clicks the button. EXAMPLE - Math.random()
PRACTICEIn the example above:
GAME DEVELOPMENTModify your game page (game.htm) as follows:
|
References |
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com |