|
|||
| week 04c - variables & variable scope, properties, Math.random() | |||
randomness : |
a simple way to make things interesting It is common in interactive applications to find that they seem very predictable and boring, responding to the user in the same way every time. The designer would like the application to respond to the user in a more natural and variable fashion to make things more interesting. For example, if the application were posing a series of challenges in a game, it might be better if it presented them in a different order each time. Or if the application draws graphics in response to user actions, it might be better if the drawing was different each time. The solution to injecting this more natural behavior into the application is to use something called a random number generator. Almost all computer languages have a function for generating random numbers, and using it in interactive applications often produces a much more interesting interaction behavior. You can use the random number generator to do some of the following things:
|
|
| Math.random() : | In Flash MX, the technique for generating a random number is using a method of the Math object. The Math.random() method (it is like a function) generates a random number between 0 and 1. To see this, put the following code into a movieClip:
|
|
| controlling the range : | Frequently a useful random number is within a range of numbers that's different from 0 to 1. For example, if we want to change the transparency of an object to some arbitrary amount, we might want to limit the range to from 0 and 100 percent (the possible range for the alpha values). To do this, we use a multiplier to amplify the result of the Math.random() method by 100 so the result will be in the range of 0 to 100. To see this, put the following code into a movieClip:
|
|
| using an offset : | In other cases, we want a range that starts at some number other than zero. For example, rather than setting the y position of the movieClip from 0 to 100 pixels from the top, we might want to set the possible y position values to between 20, and 80. To change the starting point of the range, we need to add or subtract an offset value to/from the result of the random calculation. This changes the lower end of the range by the offset value. Now there are two steps to the calculation:
The following example demonstrates using both a multiplier and an offset. It sets the y position of the movieClip to a value in the range of 20 to 80 pixels. The multiplier is 60 (i.e. 80 - 20 = 60), and the offset is 20 (to start the range at 20 and end it at 20 + 60 = 80)
|
|
| absolute vs. relative : | In the examples so far, we have been setting the absolute value of the movieClip property by making the property equal to the result of the random number calculation. But it is often more interesting to change the property relative to the current value of the movieClip property. For example, we might want to change the position of a movieClip by 10 to 30 pixels from the current position. To see this, insert the following code:
A variation on this would be to change the position of the movieClip in either a positive or negative direction (i.e. left or right for the x coordinate). To do this, we would use a negative offset of half the range so that the resulting range is half negative and half positive. For example, if we want the position to change from -20 pixels to +20 pixels, the range is 40, and the offset is -20. To see this, insert the following code:
|
| all materials on this web site © copyright 2004, Philip van Allen |
top |