Articles Archive
Articles Search
Director Wiki

Timing multiple events

July 11, 1999
by Pat McClellan

Dear Multimedia Handyman,

I have a project where I need to be able to detect and assign certain events to multiple mouse clicks. i.e. if user clicks once, event x occurs, if user clicks the third time, event y occurs, etc. The lingo dictionary only offers ways to detect the time elapsed since last click.



Dear Yinka,

Basically, what we need to do is start a timer and start counting the mouse clicks on the first mouseUp. We'll count the clicks until the timer runs out, at which time we'll know how many clicks were registered. At that point, we'll need to process the result and reset the click counter. Make sense?

This behavior gets attached to whatever it is that is to be clicked. I'm going to assume a 20 tick timer -- a third of a second. You can change this value if you want by altering the first line of the beginSprite handler in our behavior. This value is the amount of time allowed between the first and last clicks which will allow the clicks to be considered together. Note that there will always be a delay of this value (20 ticks in our case) before the system responds to a single click. That's why you want to keep the value as low as possible.

Take a look at this demo. Click the button 1, 2, or 3 times.

This movie is available for download in Mac or PC format.

Here's the behavior for this demo, with explanation below.

--mouseCounter Behavior
-- Copyright © 1999, ZZP Online, LLC
-- Free Use for Director Online readers
property pClick, pTicks
on beginSprite me
  set pTicks = 20
  set pClick = #null
on mouseUp me
  checkClick me
on checkClick me
  case pClick of
      set pClick = #firstClick
      repeat while the timer < pTicks 
        if the mouseDown then exit
      end repeat
      alert "1 click"
      set pClick = #null
        set pClick = #secondClick
        repeat while the timer < pTicks
          if the mouseDown then exit
        end repeat
        alert "Double click"
        set pClick = #null
        alert "Triple click"
        set pClick = #null
  end case

The way this behavior works is that each time the mouse is clicked (mouseUp), a handler called checkClick is called. Let's say that nothing has been clicked yet. At this point, my property called pClick (which is used to keep track of the clicks) will be set to #null. So, when the first click comes in, checkClick notices that pClicks = #null, it sets pClick to #firstClick and starts the timer, watching it until it gets up to 20 ticks. I'm using a repeat loop to keep us watching the timer until it reaches 20 ticks. When we finish with the repeat loop, in my demo, an alert box appears and pClicks is reset to #null.

Now, let's look at the alternative. If the another click occurs before the timer gets to 20, we need a way to get out of the repeat loop and NOT execute the single click result. I do this with the line ...

if the mouseDown then exit

So, if the mouse is pushed down before we've finished counting to 20 ticks, we don't get the single click alert message, and we don't reset pClick to #null. That all happened on mouseDown. When the user release the mouse (mouseUp), checkClick is called again. However, this time, pClick is now set to #firstClick, so it goes to a different (but similar) part of the handler.

This time, pClick is set to #secondClick. We start watching the timer again in another repeat loop. If we get all the way to 20 ticks, then the double click alert message occurs, and pClick is reset to #null. But if another mouseDown occurs, we exit the handler before executing the double click result. When the mouseUp occurs this third time, checkClick is called again. It sees that pClick is #secondClick, so it knows it's time to execute the triple click alert -- and reset pClick to #null.

This behavior only allows for up to 3 clicks, but you could easily continue the concept. Keep in mind that only single and double clicks are conventional, but you may need something like this for a game. Good luck with your project.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

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