Advertisement
Atriace

Namesearch: Split Arrays

Aug 11th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.text.TextField;
  2. import flash.text.TextFormat;
  3. import flash.events.KeyboardEvent;
  4.  
  5. // Style for our TextField
  6. var inputTextFormat:TextFormat = new TextFormat();
  7. inputTextFormat.font = "Consolas";
  8. inputTextFormat.size = 14;
  9.  
  10. // Create our TextField and let us type in it.
  11. var text_in:TextField = new TextField();
  12. text_in.defaultTextFormat = inputTextFormat;
  13. text_in.selectable = true;
  14. text_in.type = "input";
  15. text_in.text = "Type Here";
  16. text_in.addEventListener(KeyboardEvent.KEY_UP, keyboardHandler); // listen for the enter key
  17. addChild(text_in)
  18.  
  19. // Create our datasets.
  20. var names:Array = ["apple", "osx", "ios", "microsoft", "windows", "xbox", "linux"];
  21. var frames:Array = [2, 2, 2, 3, 3, 3, 4];
  22.  
  23. function keyboardHandler(e:KeyboardEvent):void {
  24.     var txt:TextField = e.currentTarget as TextField;
  25.     var lowercase:String = txt.text.toLowerCase();
  26.     var i:int;
  27.    
  28.     if (e.charCode == 13) { // Enter Key
  29.         for (i = 0; i < names.length; i++) {
  30.             if (lowercase == names[i]) {
  31.                 break; // stop the search, since we found the match.
  32.             }
  33.         }
  34.        
  35.         // If the break didn't occur, "i" would reach names.length
  36.         if (i == names.length) {
  37.             trace("No match found. :(");
  38.         } else {
  39.             trace("Going to frame " + frames[i]);
  40.             gotoAndStop(frames[i]);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement