Counter in a Field

Date:    Wed, 21 Dec 1994 21:10:05 EST
From:    John Dowdell <71333.42@COMPUSERVE.COM>
Subject: Re: Increment/Decrement
Mike Brown's looking, on Dec 19, to increment and decrement a counter in a field. Before getting into the task itself, Mike, there are a couple of critical things in the preliminary script you sent up:

To a lesser degree, it is also best to avoid hardwiring sprite numbers as well. This will, I am told, become even more important in the future....

A number of scripting techniques can offer the functionality you wish, while still remaining general enough for later transport or reuse.

With that important stuff out of the way, here's an implementation of the popular Windows "spinner" type of control. The following Cast Script goes into the spinner control itself.

There are three graphics:

  1. "spinner regular": the spinner in its default state (a 30x16 pixel rectangle with up/down arrows);
  2. "spinner up": a duplicate of "spinner regular" with the up button highlighted; and
  3. "spinner down": like the previous, except the down arrow is highlighted.

All graphics are the same size and are named as above. There is also a field named "counter" on the Stage, and this will display the numeric values. "Spinner regular" is displayed alongside it; the other two spinner graphics are held in reserve in the Cast. The Frame Script just contains a "go the frame" command in its exitFrame handler.

Here's the Cast Script for "spinner regular":

  on mouseDown

    --  Set all local variables:
    set chan = the clickOn
    set range = 10         -- the range over which numbers run
    set perSecond = 10     -- the number of changes per second

    --  Main loop:
    repeat while the stillDown

      if NOT (rollover(chan)) then   --  handles rolloff


        set bitmap to the number of cast "spinner regular"
      else                          --  mouse is above button
        set theVal to value(the text of field "counter")
        if the mouseV < the locV of sprite chan then      
                                     --  if above "up" button
          set bitmap to the number of cast "spinner up"
          set theVal = (theVal + 1) mod range
        else                       --  if above "down" button
          set bitmap to the number of cast "spinner down"
          if theVal then set theVal = theVal - 1          
                                    --  tests for "theVal = 0"
          else set theVal = range - 1
        end if
        put theVal into field "counter"                   --  update counter
      end if

      set the castNum of sprite chan to bitmap      --  update spinner
      updateStage

      --  Speed control:
      startTimer
      repeat while the timer < 60 / perSecond
        if NOT (the mouseDown) then exit
      end repeat

    end repeat        -- ends main "stillDown" loop

  end mouseDown

When the user presses on the spinner control, either the up or down button highlights and the numbers in the field named "counter" increment or decrement. If they roll off the button then the spinner de-highlights and the numbers don't change.

Notes:

This example goes a little further than what you proposed, Mike, by incorporating special buttons, handling rolloffs, and allowing a great range of input and timing values... in its favor, it can be created with only five castmembers (three graphics, a textfield, and the Frame Script) and two sprites. Hope it's of interest, and that it gets the job done!