Articles Archive
Articles Search
Director Wiki
 

Rollover sounds and things that go "bump" in the night

March 2, 1998
by Pat McClellan

Dear Multimedia Handyman,

Well, first of all let me tell you that I'm new to Director, and I just know the basics like "rollOvers" and "go to's". I have few books that helped me a lot but I still have a question: How do you make a button that will play a sound when rollOver, just once, without going "watwatwatwatwatwawta" and repeating forever without understanding what it is?

Thanks,

Ignacio

Dear Ignacio,

I'm thinking that if I had a button that would go "watwatwatwatwatwawta".... I'd keep it! (But I understand that that might not be best in every application.) The crux of the problem is that before the release of version 6, Director didn't have built in events for mouseEnter and mouseLeave. So to have your buttons do anything on rollOver, you had to repeatedly check for rollOver and repeatedly execute a command like set the memberNum of sprite 10 to whatever. Though inefficient because of its redundancy, the repetition is invisible to the user... until you start using sound. In that case, you're repeatedly triggering a puppetSound, so you get that annoying digital stuttering.

In my Director 5 days, I used Tab Julius' Rollover Tool Kit Xtra to get the mouseEnter/mouseLeave info. But with the release of Director 6, those events functions are built into the program. The following script will play a sound once when the sprite is initially rolled over (without repeating).

on mouseEnter me
  puppetSound "mySoundMember"
end  

Just substitute your sound's name into the script. If you've got other sounds being played (background music), you should specify a channel for the sound to play in that is not 1 or 2. Channels 1 and 2 are the ones you see in the score, but you can use up to 8. I've heard of problems on some PCs for channels above 4, though I've never needed to go above 4 so I can't validate that rumor. I'd use a behavior script like this:


on mouseEnter me
  puppetSound 3, "mySound"
end 

So what if you don't have D6 and you don't want to buy an Xtra. No problem, that's the meat of this article and the reason I picked your question. You need to use a flag. Flags are extremely simple and valuable in a multitude of situations. Once you start using them you'll wonder how you ever did without. Here's the concept.

Back in my college dorm days, my roommate and I agreed that if one of us had a guest in our room and needed privacy, we would discreetly place a rubber band on the outside doorknob. Nobody would notice, but since we knew to check for it, it would be a flag to go away until later. It worked perfectly -- though I was invariably the one on the outside of the door! Now, let's apply the concept to your situation. What we need to do is put a proverbial rubber band on the sprite when you first rollover it and then Director will know not to play the sound again until it is removed.

The flag is a global variable that we'll call gSoundFlag. Initialize gSoundFlag as FALSE in your startMovie script. Then, when the user rolls over the sprite, the frame script will check to see if the sprite is rolledOver. Then the script will check to make sure that gSoundFlag is FALSE. If so, the puppetSound will play AND gSoundFlag will be set to TRUE. By setting the flag to TRUE, subsequent iterations of the script will NOT play the sound. When the cursor leaves the sprite, rollOver is false and the else portion of the script resets the flag to FALSE so that it will make a sound next time it is rolledOver. So you'll end up with an exitFrame script like this.

on exitFrame
    global gSoundFlag
    if rollOver(x) then
        -- substitute your sprite channel
        if gSoundFlag = FALSE then ¬
            puppetSound 3, "mySound"
        set gSoundFlag = TRUE
        else
        set gSoundFlag = FALSE
    end if
end

Try the demo movie, both with and without the flag in operation.

You need <a href='http://www.macromedia.com/shockwave/'>ShockWave</a> 6 to view this movie!

I frequently use flags for other things such as tracking whether a user has been to a particular frame in a movie, whether the user has completed a quiz, etc. The example I've given uses a global variable, but if you are using behaviors or object-oriented programming, flags are generally held in properties. Either way, you'll find lots of uses for them in your work.

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.