Im trying to implement a function for the user to be able to change a desired key using the storage.csv, im able to change, save and read the key.
Everything is working as intended except that a key must be pressed before the function executes. Otherwise if the 'Greenfoot.getKey()' returns a null value my program stops working, as greenfoot does not like the function GetKeyPressed calling back on itself.
Im not sure how to go about making this subclass work that it clears the Greenfoot.getKey(), so that the class AskKeyboardInput is able to ask the user for input, while GetKeyPressed waits for a keyboard input - basically just like the usual feature of being able to change a key.
Thanks a lot
Edit: Also, i am trying to do this to 6 keys, where as the storage.csv only allows for 5 string values, is there a way i could store the last key?
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 | { public KeyboardConfigThrust() { GreenfootImage image = new GreenfootImage( 550 , 50 ); image.setColor(Color.GREEN); float FontSize = 45 .0f; Font font = image.getFont().deriveFont(FontSize); image.setFont(font); image.drawString( "Thrust Forward" , 1 , 35 ); image.drawString(MainMenu.Thrust, 350 , 35 ); setImage(image); } public void act() { if (Greenfoot.mouseClicked( this )) { getWorld().addObject( new AskKeyboardInput(), MainMenu.resX/ 2 , MainMenu.resY/ 2 ); //Greenfoot.getKey(); //clears getKey() GetKeyPressed(); } } public void GetKeyPressed() { String key = Greenfoot.getKey(); if (key != null ) { MainMenu.Thrust = key; UserInfo PlayerInfo = UserInfo.getMyInfo(); PlayerInfo.setString( 0 ,MainMenu.Thrust); PlayerInfo.store(); AskKeyboardInput.KeyInput = true ; ResetImage(); } else GetKeyPressed(); } public void ResetImage() { getWorld().addObject( new KeyboardConfigThrust(), MainMenu.resX/ 3 , (MainMenu.resY/ 16 )* 9 ); getWorld().removeObject( this ); } } |