I am makeing a java 2D game and i cant figure out how to add the wasd movement. I allready made a keyboard (wich is called "Keyboard") reader class but idk where do i add the key listener (addKeyListener(new Keyboard)) !

Add the listener to the component that has the focus.
The listener class will have to implement the KeyListener interface.

What is a WASD movement?

April 16th, 2012, 01:09 PM

Alex555

Re: WASD Movement! Need Help!

thnx man,the keyboard class should have the KeyListener interface (idk why it doesnt have it here)

WASD movement is used in almost all games. instead of using arrow keys to move you use W A S and D keys (wich is alot easier and comfutarble)

And wich of the components has the focus? is it my MainClass? if yes where exactly do i add it.

April 16th, 2012, 01:16 PM

KevinWorkman

Re: WASD Movement! Need Help!

That is way too much code to ask people to debug for you, for free, in their spare time. I highly suggest you put together an SSCCE containing only code pertinent to your question- your question is about a KeyListener, so you don't need any game logic.

Can you make a small, complete program (a SSCCE) that compiles, executes and shows the problem?
Remove all the code that does not matter for solving the problem.

April 16th, 2012, 02:01 PM

KevinWorkman

Re: WASD Movement! Need Help!

Quote:

Originally Posted by Alex555

But half of the code here doesnt really matter.(like my screen class)

Exactly, that's my point. Make it easier for people to help you.

April 16th, 2012, 02:25 PM

Alex555

Re: WASD Movement! Need Help!

I did. I said wich classes are (maybe) useless.

April 16th, 2012, 02:30 PM

Norm

Re: WASD Movement! Need Help!

If those classes are not used in the code, why are they posted?
If its so easy to make a small testing program, if you'd do it then some one could help.

The idea is, the tester copies the file, compiles it and can test with the results to see the problem.

April 16th, 2012, 03:40 PM

Alex555

Re: WASD Movement! Need Help!

Guys you didnt understand correctly. ALL classes are used. However, i think some classes,such as the Screen class, do not contain the problem, but it is good to have them there because: The idea is, the tester copies the file, compiles it and can test with the results to see the problem.

April 16th, 2012, 03:45 PM

Norm

Re: WASD Movement! Need Help!

I think we do. We're waiting for you to make a small program that we can easily copy, compile, execute and see the problem.
The idea is that you remove the useless code to make the debugging easier.
Often in the process of creating the small testing program, the OP finds the problem.

April 16th, 2012, 04:25 PM

KevinWorkman

Re: WASD Movement! Need Help!

The time you spent arguing about what we should want in order to test your code for you for free in our spare time could have been spent just giving us what we requested. You'd probably have an answer by now. Oh well, it's really your call. There are plenty of other posts here that do include an SSCCE that we can be helping in the meantime.

April 19th, 2012, 01:59 PM

Melawe

Re: WASD Movement! Need Help!

You should hear what they are asking you to do. First of all, you can learn from cutting a few lines of code from big files, and second they are trying to help you. So if they ask something you should prolly do it.