Core Objects
Math object
Math object keeps many useful mathematical methods and constants, which are extensively used in calculations. Square roots, logarithms, trigonometric functions and random numbers – all it may be done with help of Math object.
Financial and scientific applications, "computer art", games – all that cannot be imagined without Math object.
We cannot give an example of each Math method, but after completing the following exercise you will receive a good impression of what can be done.
- Create a movie with instance name thecircle
- Add three dynamic text boxes and assign variables diameter, circumference and area to them.
- Add the button with the following code
on(release) { a = Math.round(Math.random() * 150); _root.thecircle._width = a; _root.thecircle._height = a; diameter= a; circumference = Math.round(a * Math.PI); area = Math.round(Math.pow((a/2), 2) * Math.PI); } - Analyze the code. Firstly a random number is assigned to the diameter of the circle. Math.random() method generates a number beween 0 an 1, so to make it from 0 to 150 we simply multiply it to 150. As this value will be a decimal with quite many digit after the dot, we rounded it to integer with Math.round() method.
Circumference and area are calculated with use of Math.PI constant.
- Test the movie – it should display the calculated value in the text boxes after button click.