I'm making Space Invaders, and the game is running with a while-loop.When the game is not in a loop it respons to key presses, but when i have started the loop it don't respond. It's the same object that implements the keylistener and the loop method, so is it possible that they can't do both at the same time? Will i have to create another object just for keylistening so i can type in loop?

In short, this is what happens when i run the game:The main method creates a game object --> the game waits for a "any key press" (that thing works) --> the game starts-->it initializes the player object, and starts the loop.

all the comments are danish, but i believe i made all the varibles english

You need to separate your drawing code from your game code. Your loop needs to go in a Thread other than the EDT, and it can't include any of the drawing methods.

Also, the "Graphics g" variable shouldn't be an instance variable of the class. Instead, the parameter used by paintComponent should be used. Since you're ignoring repaint, you will have to call paint yourself, and paint will call paintComponent. You should look up BufferStrategy in the Java API and the Java Tutorial to see how you're supposed to use this.

I tried to fix up your code, but it got to be too much. It would be easier to just give you code than to try writing it all over again.

You should buy either Developing Games in Java or Killer Game Programming in Java (preferably both) from Amazon.com. After reading those two books, you'll probably have an idea of what to do. These will explain active rendering, which it seems like you think you're doing but you're not.

Reading a book about threads (e.g. Java Concurrency in Practice) would probably be helpful too, but it might be overkill.

why do you have the keyboard reading in another class. there is an AWT library for it, and lwjgl and slick can do it, other probably can as well. you can just implement keyboardListener and then jsut make a method for keypress keyrelease and keyTyped I think. or jsut have it check the keys every loop if you use LWJGL or Slick.

but then again the coke and code tut did a lot of the stuff it didnt need ot do.

if there is a reason for making a class to read the keys please inform me.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org