Archive for August 27th, 2006

Director, Math, and Javascript

In DirMX 2004 the ability to have scripts that make use of Javascript rather than Lingo was included. Although I’m from a Lingo background, I have found a few things that are much simpler to do in Javascript.

Lingoworkshop discusses Javascript in Director in terms of dealing with text and encryption. In my line of work one of my big concerns is display of numbers. Just like text, Javascript can help out with this.

In Lingo I’m sure you are used to seeing things such as this in the message window:
the floatPrecision = 4
put 3/4
-- 0
put 3./4
-- 0.7500

The first example is due to division of integers. In a math class 3/4 should not be equal to 0. In the latter example you are left with extra zeros due to the floatPrecision. In both cases I wouldn’t want my output text to show either of those values. I’d really like it to show 0.75.

Along comes Javascript. If you flip your message window over to Javascript you can type the following:
eval(3/4)
0.75
eval(3/5)
0.6
eval(3/7)
0.42857142857142855

This looks much better to the part of my brain that thinks in math. Using eval() makes getting simple expressions for display relatively easy. In Director there are ways to get these nicely formatted numbers, but nothing quite as quick as using the Javascript (from a coding point of view, I’ve never done any tests on the speed of this Javascript within Director).

One real world example where this can be used is in a basic calculator. In this particular case I did a search for “javascript calculator” and found many different implementations of Javascript for this purpose. I took the code and made a few modifications, added a few buttons, and one behavior (in Lingo) to deal with keys getting hit, and the final product can be used to do your taxes…or at least divide 3 by 4 with a nicely displayed number.

Click to view example (pop up window)

Download this example (zip file, 100k)

Add comment August 27th, 2006


Director Sites

Calendar

August 2006
M T W T F S S
« Jul   Sep »
 123456
78910111213
14151617181920
21222324252627
28293031  

Archives

Recent Posts

Recent Comments