• Post Reply Bookmark Topic Watch Topic
  • New Topic

All diagonals via KeyEvent working but one.  RSS feed

 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy there. I was struggling with trying to implement diagonal movement via KeyEvents and finally thought I had it until I noticed one diagonal, being the W+D (up+right), would not work.
Changing the W or D key in the situation of both being pressed fixes it, but I am trying to retain the wasd movement. When both are pressed no diagonal movement occurs, but independently they work just fine. I've been stuck on this for a while with no clue how to go about fixing it. I am able to provide more if my question is unclear. Any and all suggestions are appreciated!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When both are pressed no diagonal movement occurs,

How are you keeping track of which keys are currently pressed? The keyPressed method will tell you the key that was just pressed. The code needs to remember what keys have been pressed and not released to detect when more than one key is pressed.

If the other diagonal moves are working, what is different for the one that does not work?

How are you debugging the code to see what is happening when it executes? I use print statements to print out the values of variables as they are set and used.

Note: Comments in the code would help others see what the code's logic is.
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keeps track, and is removed in the keyReleased method. As said the other 3 diagonals do work but the W + D does not.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the other 3 diagonals do work but the W + D does not.

What is different for that combo of keys?

How are you debugging the code to see what is happening when it executes? I use print statements to print out the values of variables as they are set and used.

Note: Comments in the code would help others see what the code's logic is.
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just run the build of my game to test everything. Nothing is different in the setup of this diagonal to the others. I'm unsure why the W + A diagonal works but the W + D does not. Same for the D + S, both keys work in conjunction with others but not when used together
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you debugging the code to see what is happening when it executes? I use print statements to print out the values of variables as they are set and used.
What is in keys after line 7?

Note: Comments in the code would help others see what the code's logic is.
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unsure how to answer that question. I use the console to see fps, ticks, etc if that is what is being asked. I tried to use a print statement and the line I wanted to print did not come out, so unsure where the problem lies.
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keys uses pressed to have whatever key is pressed added to it and then acts upon seeing which key(s) are pressed.
Oh I use Eclipse if that is of any help.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is in the array: keys when the code is executed? Use the Arrays class's toString method to see:
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an ID [68, 87] is what comes when W + D are pressed.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're using a TreeSet and your keys are probably in the wrong order. Aside from the quick fix of swapping W and D, there's no reason to check if you're pressing two buttons at the same time. Just decouple the horizontal and vertical directions:
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot believe I didn't think of just switching the letters around considering D changes the x and W changes my y... wow /.- Thank you. Your option is probably much more efficient but I am having a hard time understanding some of it.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard time understanding some of it.

Are the coding standards today to NOT use comments? Is there a problem when a programmer adds comments to his code?
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! I'm just INCREDIBLY new and this is very incomplete. I shall try to make it a habit in the future to add comments to my code.
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I understand most of the logic in Stephan's suggestion just a few things included I haven't used before.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's easier for you to understand if we do away with the maps and the enums:
 
Andrew Holifield
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Switch statements also make the code much cleaner in comparison lol. I will try to implement this. I appreciate the suggestion, and thank you to Norm as well for getting me to instill a better programming habit
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree. I avoid switch statements like the plague. If this were my application, I would use the variation that uses enums and maps, because it's more flexible and configurable, except I'd document it better, as Norm has suggested.

If I'm tempted to write a switch statement, it tells me my code is too rigid.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!