How to add sound to a typewriter effect code in Actionscript 2.0

258 Views Asked by At

My problem is that I want to implement a keystroke sound (it's called "TextBeep" and the linkage name is also "TextBeep" it is a .WAV file) while a typewriter effect text is being played. As soon as all of the text appears, the sound should stop.

How do I do this? I have been looking for ages but there is only ones for ActionScript 3.0 and nothing for the code I am using. I am using Adobe Flash CS4.

Here is the code I am using:

    var effectTxt:String = _root.effect.text;
    _root.effect.text = "";
    var startEff:Number = 1;
    _root.onEnterFrame = function() {
        if (effectTxt.length>=startEff) {
            _root.effect.text = effectTxt.substr(0, startEff);
            startEff++;
        }
        else {
        delete _root.onEnterFrame}
    };

If someone can tell me how to get it so it can automatically start as soon as the frame begins and the text start appearing and then to stop the sound after all text has appeared that would be great. Also, if you don't mind could you explain how you did it for future projects. Thanks!

1

There are 1 best solutions below

0
Nadia On

so the sound effect is not a single "click" but multiple and long enought to not need a loop, right? Rather than enterFrame, use setInterval, you can better control typing speed...

var effectTxt:String = _root.effect.text;
_root.effect.text = "";
var startEff:Number = 1;
var firstLetter:Number = 0;
mySound = new Sound();
mySound.attachSound("TextBeep");
writeText = setInterval(write, 100); // 100 means one character every 1/10 of second


function write ()  {
// launch the sound just at the first letter
firstLetter != 1 ? mySound.start() : null;
// use start(0,10) if you need to loop it ten times.
firstLetter =1;
    if (effectTxt.length>=startEff) {
        _root.effect.text = effectTxt.substr(0, startEff);
        startEff++;
    } else {
      mySound.stop();
     clearInterval(writeText);
};