Math and Numbers

A couple of tips on dealing with math in Director.

JavaScript Math

If Lingo doesn't have a particular math function you needarcSin, for instanceyou can use JavaScript's math library. Simply create a new movie script and set its type to JavaScript, instead of Lingo. You can then create your own math function in JS like so:

 function arcSin (num) {   return Math.asin(num); } function arcCos (num) {   return Math.acos(num); } 

Once created, the functions can be called like any method, even from within Lingo:

 on test num   as = arcSin(num)   trace(as) end 

Integer Numbers

Here is a question that repeatedly appears in the newsgroups: Why do certain calculations produce incorrect results? The answer is always the same: you're doing integer math, and Director rounds integers. Use floats instead.

Look at this example from the Message window:

 trace(21 / 5) -- 4 

To get the correct result turn one of the numbers into a floating-point, or real, number:

 trace(21 / 5.0) -- 4.2000 

Or use the float method:

 trace(21 / float(5)) -- 4.2000 

Macromedia Director MX 2004. Training from the Source
Macromedia Director MX 2004: Training from the Source
ISBN: 0321223659
EAN: 2147483647
Year: 2003
Pages: 166
Authors: Dave Mennenoh

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: