I was wondering what the best way would be to implement the usage of cheat codes in say.. my main menu. No UI. The user just types. If the user doesnt type something in a given amount of time the string resets. Hoping for suggestions and that it might help someone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import greenfoot.*; public class MyWorld extends World { private String key = "" , code = "" ; private int wait = 0 ; public MyWorld() { super ( 600 , 400 , 1 ); } public void act() { if (wait > 0 ) wait--; else code = "" ; if ((key = Greenfoot.getKey()) != null ) { wait = 60 ; code = HandleKey(key); key = "" ; } switch (code.toLowerCase()) { //this is the part you actually check for the entered string. default : break ; case "unlockall" : //unlock All break ; case "easterEgg" : //something break ; } } private String HandleKey(String key) { switch (key) { default : return code+key; case "space" : return code+ " " ; case "backspace" : return (code.length() > 0 ) ? code.substring( 0 ,code.length()- 1 ) : code; //simulate backspace(remove last character from string if possible) } } } |