Skip to main content
November 10, 2015
Answered

Typewriter effect with a button

  • November 10, 2015
  • 1 reply
  • 1056 views

Hello... hm, i would like to have a button, a dynamic text and an input text.

When i will click on the button, the input text will be show in the dynamic text, with a typewriter effect.

I found this script... (a cool script with a sound, when the typewriter effect works)

var mysound =new kb();  

mysound.play();

var myString:String = origine.text;

var myArray:Array = myString.split("");// seperates each letter

addEventListener(Event.ENTER_FRAME, frameHandler);

function frameHandler(event:Event):void {

    if (myArray.length > 0) {

    resultat.appendText(myArray.shift());

    } else {

      removeEventListener(Event.ENTER_FRAME, frameHandler);

      SoundMixer.stopAll();

    }

}

So it works when i launch the swf, but... I don't know the correct way to make it with a button...

bouton.addEventListener(MouseEvent.CLICK, gogogo);

function gogogo(event:MouseEvent):void {

//???

}

I don't find it. Any idea, please?

This topic has been closed for replies.
Correct answer Ned Murphy

Just move the ENTER_FRAME event listener and the sound play command inside the button's event handler

  1. bouton.addEventListener(MouseEvent.CLICK, gogogo);  
  2. function gogogo(event:MouseEvent):void {  
  3.           addEventListener(Event.ENTER_FRAME, frameHandler);
  4.           mysound.play();

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
November 10, 2015

Just move the ENTER_FRAME event listener and the sound play command inside the button's event handler

  1. bouton.addEventListener(MouseEvent.CLICK, gogogo);  
  2. function gogogo(event:MouseEvent):void {  
  3.           addEventListener(Event.ENTER_FRAME, frameHandler);
  4.           mysound.play();
November 10, 2015

Yeah thanks Ned Murphy, it works , but it make a new problem, it works only one time. When i write another text in the input text and i click on the button : nothing happen ... I tested to move another things but : none. It's not only for a one time... i will try to move another things again, to see

Ned Murphy
Legend
November 10, 2015

Then you need to also include more in that function.  You need to assign the myString and myArray variables inside that function before you start the ENTER_FRAME rather than outside of it like you have now, though you still need to declare the variables outside so that both functions have access to the variables.  Rather than do it for you see if you can figure out how to do it based on what I just explained.