Advertisement
Atriace

Namesearch: Object/Array

Aug 11th, 2017
259
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 an object with arrays for each key.
  20. var list:Object = {
  21.     "2":["apple", "osx", "ios"],
  22.     "3":["microsoft", "windows", "xbox"],
  23.     "4":["linux"]
  24. }
  25.  
  26. function keyboardHandler(e:KeyboardEvent):void {
  27.     var txt:TextField = e.currentTarget as TextField;
  28.     var lowercase:String = txt.text.toLowerCase();
  29.     var answer:int = -1;
  30.    
  31.     if (e.charCode == 13) { // Enter Key
  32.         for (var key:String in list) {
  33.             if (answer != -1) { break; } // Stop the search if we found a match.
  34.            
  35.             for (var i:int = 0; i < list[key].length; i++) {
  36.                 if (lowercase == list[key][i]) {
  37.                     answer = int(key);
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.        
  43.        
  44.         // If the break didn't occur, "i" would reach names.length
  45.         if (answer == -1) {
  46.             trace("No match found. :(");
  47.         } else {
  48.             trace("Going to frame " + answer);
  49.             gotoAndStop(answer);
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement