# Creating an Odometer

February 27, 2001

by Will Turnage

Dear Multimedia Handyman:

I need to create a scrolling field which has the numbers 0 to 9 scrolling automatically like a gas station counter, depending on the value of the global variable stored in it.

Ravi

Dear Ravi:

If you want to create an odometer, you'll need to start by creating a separate text member for each of the numbers you want to display. So if you want your odometer to only show six numbers, then you'll need six separate text members.

The next part, which is also the most tricky, involves breaking down a number into its different factors. For instance, if you had the number 5629, and you wanted to break that number down into six factors, you would need to figure out how to break that number down into the series 0, 0, 5, 6, 2, and 9.

To break a number down into pieces like this, you should use the mod function in Lingo. The mod function takes two integers and divides the first integer by the second integer. Then it returns the remainder of that division. So 103 mod 8 would equal 7 because 103 divided by 8 is 12 with a remainder of 7.

In the previous example, you can take the number 5629 and calculate 5629 mod 10 which returns 9, or the value of the number in the ones column. If you subtract 9 from 5629, you'll get 5620, and then you can calculate 5620 mod 100, which returns a result of 20. Then you can subtract 20 from 5620 and get 5600, and then you can calculate 5600 mod 1000 which returns 600. If you continue this process for each place in your number, you end up with a set of simpler numbers which all add up to your starting number. In this example, the set of simpler numbers you would create are 5000, 600, 20, and 9.

The final step is to take this set of numbers and eliminate the zeroes in each number so that you're left with a single digit. Simply divide 5000 by 1000, 600 by 100, 20 by 10, and 9 by 1, and you'll end up with the four numbers 5, 6, 2, and 9 that make up your original number 5629.

But enough talking about math, let's look at some code. In your project you should create a handler that reads in a number and returns a list of the individual digits that make up that number. The handler might look like this:

on createIndividualNumbers whichNum, numberOfPlaces

tempList = []

repeat with i = 1 to numberOfPlaces

factor = integer (power(10, i))

currentNumber = whichNum mod factor

tempList.addAt (1, currentNumber * 10 / factor)

whichNum = whichNum - currentNumber

end repeat

return tempList

end

Start by passing the handler two arguments. The first argument is the number that you are going to break down, and the second number is the number of places that you want to calculate for. So if you wanted to break down 35629 to 7 places, the function would return [0,0,3,5,6,2,9].

The handler creates an empty list to store the individual numbers. Next, it begins a repeat loop that will execute for each place in your number. The repeat loop will start at the end of the number and work towards the beginning of the number.

For each iteration in the loop, the first line calculates the factor used for the current place in your number. The first time through, your factor is 10, then 100, then 1000, and so on. The next line takes your total number and calculates the mod between it and the new factor. Next, you take this simple number, eliminate the zeroes from it, and add the single digit to your list. Finally, you subtract the value of the current place from your total number for the next time in the repeat loop.

To use this in your project, all you need to do is constantly call this function whenever you have a new number you want to break down. Then, just take the list of single digit numbers and put their values into each of your separate text members. The end result will look something like this:

A sample Director 7 movie is available for download in Macintosh or Windows format.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.