Articles Archive
Articles Search
Director Wiki

ScrollTop triggered member swap with sound

January 2, 2000
by Pat McClellan

Dear Multimedia Handyman,

I've been creating a small example where it is possible to change a sprite's castmember on stage by scrolling a text sprite. It works fine with this script:

on exitframe
  if member(1).scrollTop >= 0 then set the member of ¬
    sprite 4 = 5
  if member(1).scrollTop >= 90 then set the member of ¬
    sprite 4 = 6

I've also built in a possibility to scroll with the arrow keys Up and Down. My question is if it is possible in a "simple way" to also play a puppetsound everytime a new picture appears on stage(just a little "Ding"). I can start a sound but I can't prevent looping it!

Something like this:

if the member of sprite 4 = member 6 of CastLib 1 then 
  puppetsound 1 "Ding" 
end if

Am I all wrong?

Peter Lissenko

Dear Peter,

Think about when you want things to happen. Right now, you have the member resetting itself every frame -- even if it is in the correct spot. You also have it evaluating multiple if statements, when it really could go into a single (somewhat extended) if-then-else if statement. As it is now, let's say that you have 12 possible members, so you'll have it check for the value being greater than or equal to 0, 90, 180, 270, 360, 450, 540, 630, 720, 810, 900, 990, and 1080. If the scroll happens to be at 1081, then ALL of the if statements will evaluate to true, and your sprite will get its member reset 12 times EVERY frame. That's not very effecient.

Generally, it's a good idea to check for the values in the order of most specific down to least. So if you're checking for greater than, you'll want to check the values from highest down to lowest... like this:

scrollValue = member(1).scrollTop
if scrollValue >= 1080 then
  do whatever
else if scrollValue >= 990 then
  do something else
else if scrollValue >= 900 then
  another alternative
... etcetera
end if

Note that I set the variable scrollValue equal to member(1).scrollTop, then use the variable name for the rest of the handler. This is marginally faster because the Director only has to check to see what the scrollTop value is once, rather than for each of the evaluations. It also saves you some typing and makes your code a bit clearer.

I've used this reverse order approach in instances where I was checking the movieTime of an audio only quicktime sprite, cueing the score to go to specific frames when the quicktime movieTime value was greater than or equal to certain cue points.

For multiple if-then checks, I usually prefer to use a case statement (check your Lingo Dictionary if this is new to you.) Case statements only work for checking the equivalency, so when you need to check for greater or less than, I don't know of any way to use a case statement.

However, since your scrollTop intervals are very regular intervals of 90, I'd like to suggest a completely different approach. Try breaking it down into a simple math formula. Use the fact that when dividing integers, Lingo drops remainders... so

put 989/90
-- 10
put 990/90
-- 11
put 1080/90
-- 12
put 1083/90
-- 12
put 1099/90
-- 12

Then it's just a matter of converting that value into a cast member. Assuming that your first member is 5, the formula will be:

on exitFrame
  currentTop = member(1).scrollTop
  lineInterval = 90
  memberOffset = 5
  whichMem = (currentTop/lineInterval) + memberOffset
  currentMem = sprite(4).member
  if currentMem <> member whichMem then
    sprite(4).member = member whichMem
    puppetSound 3, "yourSoundHere"
    end if

This approach requires you to have all of your members in consecutive order. And if you move them, then the code will break... so be careful. You could name the first of the cast members something like "firstmember". Then set the memberOffset equal to the memberNum of member("firstmember"). That way, you can move the cast members without breaking the code -- but keep them in the correct order if you move them.

Note how I've set up the if statement. It only re-sets the member of the sprite if it is different from the current member. I lump the sound command in there too. That way, you don't get the sprite repeatedly changing cast members and looping the sound.

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