Articles Archive
Articles Search
Director Wiki
 

Link matching sprites

November 16, 1998
by Pat McClellan

Dear Multimedia Handyman,

I'm making a matching game, where the user must click a sprite, then click the matching sprite within 2 seconds. I am using global varables to store the matching values, but I'm not sure how to do the timing part. I've tried to use "the short time", but I can't figure out a good way to subtract one time from the other. Can you help?

Kev

Dear Kev,

Calculating differences in time based on the clock functions in Director is very complicated -- though I don't really understand why this has to be. (The time is always output as a string, which doesn' make much sense to me.) Anyway, we don't need to worry about it, because we won't be using the short time. We don't really need to know what time it is. Rather, we need to know the time lapse between one event and the next. This is more like needing a stopwatch than a clock. Luckily, that's very easy to do, using the timer function.

The timer is a free running clock that measures time in "ticks" -- 1/60th of a second. The only commands you need to use is "startTimer", which resets the timer to 0. At any time after that, you can simply check the value of the timer to see how many ticks have passed since the timer was started.

Let's write a behavior to take care of this little challenge. First, let's figure out exactly what the behavior needs to be able to do. When you drop the behavior on the sprite in authoring, you'll want to be able to "name" it such that matching sprites will share the same name. You'll also want to be able to specify the time lapse (in ticks -- 60ths of a second.) This will let you recycle the same behavior for any number of pairs (or groups) of sprites. Next, during play, when the sprite is clicked on, it will need to do the following:

  1. Check to see if it is the first or second sprite of a pair to be clicked.
  2. If it is the first of the pair, it will need to store its group name in the global variable and start the timer.
  3. If it is the second to be clicked, it will need to see if its group name matches the one stored in the global variable.
  4. If the group name matches, then it needs to check the timer to see if the timelapse is less than the allowed period of time.
  5. After the second sprite is clicked and evaluated, the global variable needs to be reset to null.

So here's the behavior (written with the handy Behavior Writer Xtra from Roy Pardi)


-- timedMatch Behavior
-- copyright © 1998, ZZP Online, LLC
property pGroup 
property pTimelapse
global gClickGroup
on getPropertyDescriptionList
  set d = [:] 
  addProp d, #pGroup, [#default: #first, #format: ¬
    #symbol, #comment: "Name of match group:"]
  addProp d, #pTimelapse, [#default: 120, #format: ¬
    #integer, #comment: "Ticks between clicks:"]
  return d
end
on mouseDown me
  if gClickGroup = #null then
    set gClickGroup = pGroup
    startTimer
  else
    if gClickGroup = pGroup then
      if the timer < 120 then
        alert "This is a match!"
      else
        alert "Too slow."
      end if  
    else
      alert "No match."
    end if
    set gClickGroup = #null
  end if 
end 
on getBehaviorDescription
  set description = "On mouseDown, this behavior ¬
    tests its group name against the previous data ¬
    in a clickGroup variable.  If it matches AND has ¬
    been clicked within a set period from the last ¬
    click, then a match is made."
  return description
end

 

The guts of the behavior is the mouseDown handler, which is really a simple set of nested IF statements. Good luck with your program.

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-2024, Director Online. Article content copyright by respective authors.