The Code
The following code achieves this (assuming
the code in is also
present and that the Submit button's
onRelease( ) event handler invokes the function
entered( )).
function entered( ) {
var newWords:Array = new Array( );
newWords = myText_txt.text.split(" ");
for (var i = 0; i < newWords.length; i++) {
if (myText.indexOf(newWords[i].toString( )) == -1) {
// New word not in dictionary,
// so add it and re-sort the dictionary.
myText += " " + newWords[i];
dictionary.push(newWords[i]);
dictionary.sort( );
}
}
Lines 2 and 3 of this function create a new array,
newWords, consisting of all the words in the text
field myText_text. If you had multiple text fields
in your form, you could simply concatenate them all into one string
with something like this instead of the current line 3:
newWords = (myTextField1_txt.text + " " +
myTextField2_txt.text).split(" ");
The for loop's body (lines
4-11) searches the dictionary for every word in the text field (you
could, of course, limit it to words with three or more letters). If
the new word is not found, it is added to the dictionary, and the
dictionary is re-sorted.
Although this code uses the text in the string
myText for a fast search , it uses the
dictionary array to store new words . You can see this in action
if you test the movie in Debug Movie mode (Control→Debug
Movie) by looking at the array _root.dictionary as
you enter "aardvark." When you
click the Submit button, you will see that
dictionary[0] now stores the word
"aardvark." If you backspace until
you have deleted "aardvark" and
then start to retype it, Flash autocompletes the word once you enter
"aa".