Monday, December 30, 2013

how can i create a string that show what key the user pressed?
















hello,















so im realy new to java, and i only know the basics like int and Strings n constructors(just a little bit) and stuff like how to user other classes and yeh,















and then i watched some tutorials on how to create a game using JFrame, and now i am working on a basic game, just like mario and i wanted to show what the user pressed if the user pressed "f1" first.















so what i did was i made a paint method and i did like this: "g.drawString(keyPressed, keyPressedX, keyPressedY);" and then i created the "keyPressedX" & "keyPressedY" and i made them -1000 by default.















and then i went and created (watched alot of tutorials untill i figured it out xD ) a class that checked for userInput and i said "public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode();}" and then i did this(in the method):















"if(keyCode == e.VK_f1){ keyPressedX = 10; keyPressedY = 40;}"















and well it worked, i got the text to appear when i pressed "f1", but now i have to do the harder stuff witch is making the text be something like this: ("keyPressed: " + keyCode);















(keyPressed + whatever i pressed)















but i have no idea how to do that, i'v tried to create an String called userPressed that was equals to (""); (nothing) by default and then i went to the method that changed the x and y when i press "f1" and i said: userPressed = ("keyPressed: " + keyCode); and as i said, i have no idea how i can make this so i just tried stuff like: ("keyPressed: " + keyCode.getKeyCode()); and just random stuff like that, but ofc non of the stuff i tried worked
















-so is there anyone who can tell me how i can make it so that when u press "f1" the user will see a text saying like "keyPressed: (key)" ????
















thanks :D































No comments:

Post a Comment