package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class Text extends MovieClip {
// Declare variables ////////////////////////////////////////////////////////////////
var myString:String = "Humpty Dumpty sat on a wall.";
var myStringCopy:String = myString;
var myWordArray:Array = myStringCopy.split(" ");
var mySyllableCountArray:Array = [2, 2, 1, 1, 1, 3]; // need a syllable counting function returns mySyllableCountArray, adds one syllable for comma and 2 for period
var myTimingArray:Array = new Array;
var myStringLength:int = myString.length;
var myWordCount:int = mySyllableCountArray.length;
var myIndexArray:Array = new Array;
var myTextField:TextField = new TextField();
var myHighlightOnTimer:Timer;
var myHighlightOffTimer:Timer;
var myOnDelay:int = new int;
var myOffWordCount:int = 0;
var myOffDelay:int = new int;
public function Text() {
// constructor code
// text formating /////////////////////////////////////////////////////
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Arial";
myTextFormat.size = 22;
myTextFormat.align = "left";
// create text field ///////////////////////////////// /////
myTextField = new TextField();
myTextField.width = 1000;
myTextField.y = 50;
myTextField.x = 10;
myTextField.selectable = false;
myTextField.defaultTextFormat = myTextFormat;
myTextField.textColor = 0x000000;
myTextField.text = myStringCopy;
addChild(myTextField);
// create listen button
var myMovieClip:btnListen = new btnListen();
myMovieClip.x = 100;
myMovieClip.y = 150;
addChild(myMovieClip);
myMovieClip.addEventListener(MouseEvent.CLICK, clickListen);
myMovieClip.buttonMode = true;
// Find index locations between words
findWordIndex();
//Calculate timing
calculateTimingArray();
} // end constructor function Text////////////////////////////////////////////////////////////////////////////////////////////
// finds spaces between words and records string index locations
function findWordIndex(){
var myIndex:int = 0; // captures locatin of spaces between words, Begins at 0 to mark beginning of first word
var i:Number = 0; // Counter
do { //
myIndexArray[i] = myIndex; // records index locations to array
myIndex = myStringCopy.indexOf(" ", myIndex+1); // moves to next character in string to start search for next space, returns -1 if no space found then will exit loop
i++; // moves to next character
if (myStringCopy.charAt(myIndex) == " "){ // if next character also a space
myIndex++; // then moves to next character in string
}
} while ( myIndex != -1); // exit do while loop when myIndex -1
myIndexArray[i] = myStringLength; // Records end of last word in string
}
function calculateTimingArray (){ /////////////////////////////////////////////////////////////////////////////////////////////////
// calculate myTimingArray based on syllable count
for ( var i:int = 0; i < mySyllableCountArray.length; i++){
myTimingArray[i] = 90 + (mySyllableCountArray[i] * 150)
};
}
function clickListen(event:MouseEvent) { ///////////////////////////////////////////////////////////////////////////////////////////
// reset timers if they have been started
if (myHighlightOnTimer != null){
myHighlightOnTimer.stop();
myHighlightOnTimer.reset();
};
if (myHighlightOffTimer != null){
myHighlightOffTimer.stop();
myHighlightOffTimer.reset();
trace("myHighlightOffTimer.reset");
};
// reset counters
myOffWordCount = 0;
myOnDelay = 1000;
// set text field color to black
myTextField.textColor = 0xF000000;
// start myHighlightOnTimer
myHighlightOnTimer = new Timer (myOnDelay, myWordCount);
myHighlightOnTimer.addEventListener(TimerEvent.TIM ER, onHighlight);
myHighlightOnTimer.start();
}
function onHighlight(event:TimerEvent){ ///////////////////////////////////////////////////////////////////////////////////////////
if (event.currentTarget.currentCount == 1){
trace("onHighlight first tick");
}
// Set Text highlight format
var highlightFormat:TextFormat = new TextFormat();
highlightFormat.color = 0xFF0000;
// Highlight text
var myHighlightCount = event.currentTarget.currentCount;
if (myHighlightCount <= myWordCount){
myTextField.setTextFormat(highlightFormat, myIndexArray[myHighlightCount-1], myIndexArray[myHighlightCount]);
} else {
trace ("function onHighlight: myIndexArray out of bounds: " + myHighlightCount);
}
// start myHighlightOffTimer with variable delay
myOffDelay = myTimingArray[event.target.currentCount-1] + 400;
myHighlightOffTimer = new Timer (myOffDelay, 1);
myHighlightOffTimer.addEventListener(TimerEvent.TI MER, offHighlight);
myHighlightOffTimer.start();
// change myHighlightOnTimer delay if not at the last tick
if (event.target.currentCount < myWordCount){
myOnDelay = myTimingArray[event.target.currentCount - 1]; // offset by 1, timing array tells when to highlight next word by giving enought time to read previous word
myHighlightOnTimer.delay = (myOnDelay);
} else if (event.target.currentCount == myWordCount){
// last time through don't need to change delay for next tick
} else { // catch array out of bounds
trace ("function onHighlight: myTimingArray out of bounds" );
}
}
function offHighlight(event:TimerEvent){ //////////////////////////////////////////////////////////////////////////////////////////
// set highlightOffFormat
var highlightOffFormat:TextFormat = new TextFormat();
highlightOffFormat.color = 0xF000000;
// remove text highlight
if (myOffWordCount < myWordCount){
trace ("myOffWordCount " + myOffWordCount);
myTextField.setTextFormat(highlightOffFormat, myIndexArray[myOffWordCount], myIndexArray[myOffWordCount+1]);
myOffWordCount++;
} else { // catch array out of bounds
trace ("function offHighlight: myIndexArray out of bounds: " + myOffWordCount);
}
}
} // end class Text
} // end package