# Flash MX action script bible - Reinhardt R.

ISBN: 0-7645-4354-7

**Download**(direct link)

**:**

**164**> 165 166 167 168 169 170 .. 427 >> Next

As you have already seen, ActionScript operators take care of all the fundamental mathematical operations such as addition, subtraction, multiplication, and division. The Math class, therefore, does not concern itself with such things. As you will learn in this chapter, you can use the Math class to generate random numbers, perform trigonometric or exponential functions, and much more.

Performing ActionScript Math

You might be asking yourself, “What in the world could I ever use math for in Flash?” Well, as you will see in this chapter, you can do some pretty amazing stuff using math to power your Flash movies. Of course, it is not appropriate in every scenario. But if you want to create a project that can calculate areas of objects or even a simple interest-bearing account, you need the Math class. But what is even more important is how you can use Flash in your movies to create visual effects. Animations can occur on mathematically determined paths. And the Math class is key when you want to create artwork with the Drawing API (see Chapter 10).

Physics studies how things move, among other things. And at the heart of physics (at least Newtonian physics) is mathematics. There is no way around it. So, if you want to move things in your Flash movie — controlling them with ActionScript — and you want to bring more life into them, you need to master how to use mathematics in your code.

In this chapter, you will see how to use the properties and methods of the Math class in your Flash movies. It is not enough just to know which method to use to take one number to the power of another.

You need to know how to apply it in the context of your Flash movie.

♦ ♦ ♦ ♦ In This Chapter

Discovering when to use math in your Flash applications

Making code more readable using with statements

Generating random numbers

Working with trigonometric functions

Working with additional constants and methods of the Math class ♦♦♦♦

Learning About ActionScript Math

As with several other classes in ActionScript, Math i s a static class. This means that you never instantiate Math objects. You never create

326 Part IV ♦ The Core Classes

a Math object using new and a Math constructor method. Instead, you access the properties and methods directly from the class:

Math.propertyOrMethod;

This makes sense when you look at the functionality that the Math class makes available. The Math class essentially does little more than group together a bunch of related mathematical functions and constants. There would not be a need to create multiple instances of Math objects simply to find the cosine of an angle, for instance.

Utilizing the with Statement to Make Code More Readable

When you work with the Math class, it is not uncommon to use it many consecutive times, accessing various properties and methods. For instance:

var hyp = 10; var angle = 60;

var radians = (Math.PI * angle)/180; var yCoor = hyp * Math.sin(radians); var xCoor = hyp * Math.cos(radians);

In just that short example, the Math class was used three times. You can save yourself a lot of typing and make your code more readable simply by using the with statement around the whole block of statements. In doing so, ActionScript assumes the Math part of any operand:

with(Math){ var hyp = 10; var angle = 60;

var radians = (PI * angle)/180; var yCoor = hyp * sin(radians); var xCoor = hyp * cos(radians);

}

This example yields the same result as the previous, but it is a bit easier to read. Using the with statement is entirely optional, however. It provides no additional functionality. The idea is that it simply saves you from having to type and read Math over and over again for a block of code.

Working with the Math Constants

There are a handful of mathematical constants you can access directly from the Math class. Table 13-1 shows a list of the properties and their values.

Table 13-1: Math Constants

Property Value Description

E LN10 ~2.718 Base of natural logarithm ~2.302 Natural logarithm of 10

Chapter 13 ♦ The Math Class 327

Property Value Description

LN2 ~0.693 Natural logarithm of 2

LOG10E ~0.434 Base-10 logarithm of E

LOG2E ~1.442 Base-2 logarithm of E

PI ~3.142 n

SQRT1_2 ~0.707 Square root of 1/2

SQRT2 ~1.414 Square root of 2

The Math class constants are the values of frequently used numbers in mathematics. However, with the exception of PI, it would be entirely possible to obtain the rest of the values by means of the methods of the Math class. That fact, along with the fact that the value n happens to be central to a great many operations, makes PI perhaps the most important of the properties of the Math class.

Finding Absolute Values

The absolute value method abs() takes a single parameter — a number. It returns the distance of that number from 0. In other words, any positive value returns itself. Any negative value returns itself negated (made positive).

It can be useful to use abs()for determining whether a value is between a positive and negative counterpart. For example, the following if statement

**164**> 165 166 167 168 169 170 .. 427 >> Next