Director, Math, and Javascript
August 27th, 2006
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)
Entry Filed under: Demo Files,Javascript
No Comments yet Add your own
1. Chris Griffith | August 14th, 2006 at 11:30 am
There will be some of us Director folks there (like me). I am Looking forward to meet some more Director folks, as well. Keep an eye on Darrel Plant’s blog to see what might be up. INM typically hosts a little gathering one night.
See you in the desert!
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed