-4

Java Coding come

posted in Off Topic
Comments:
Threaded Linear
#1
61Hiko

Started a 2d Game project and I'm using KeyListener for the input/movement.
What I realised is, that vertical movement has priority over horizontal movement.
Holding W or S and then pressing A or D does nothing, the other way around though Holding A or D and then Pressing W or S does change the direction.

Put my code in chatgpt and bro says there is nothing wrong about my code and KeyListener is like that. Anyone got experience?

#2
RedeemTheCodeSir
-4
Frags
+

C++ clears lil boi

#3
Hakujitsu
-3
Frags
+

C++ brother

#4
noSananoLife
1
Frags
+

Try deepseek but use a Outlook email to create a account because a gmail wont work. it works better than chatgpt to solve complicated coding problems, just activate Deepthink

#5
EseemedRes1180
0
Frags
+

If im not wrong you are listening only for one key press so it just takes the latest key press and not multiple key presses so
track which keys are pressed using a Set or `boolean[]`.
Check that set during your game loop to handle multiple simultaneous key presses.

If this doesn't make sense just drop my text in gpt it should guve a more concise explanation

#6
Laundry
-1
Frags
+

Drop the code using the </> icon but it sounds like you are using keytyped instead of keypressed or sthm

#7
61Hiko
0
Frags
+
EseemedRes1180 [#5]

If im not wrong you are listening only for one key press so it just takes the latest key press and not multiple key presses so
track which keys are pressed using a Set or `boolean[]`.
Check that set during your game loop to handle multiple simultaneous key presses.

If this doesn't make sense just drop my text in gpt it should guve a more concise explanation

it doesn't recognize the later though, if I start with horizontal key A, D, left, right.

A/D -> W = W
W/S -> A = W/S

This what happens

#8
61Hiko
0
Frags
+
Laundry [#6]

Drop the code using the </> icon but it sounds like you are using keytyped instead of keypressed or sthm

my keytyped is empty.

#9
Laundry
0
Frags
+
61Hiko [#8]

my keytyped is empty.

Okie, would still be helpful to be able to see it to tell the problem

#10
EseemedRes1180
0
Frags
+
61Hiko [#7]

it doesn't recognize the later though, if I start with horizontal key A, D, left, right.

A/D -> W = W
W/S -> A = W/S

This what happens

I think its a problem with your if else statement try using only if statments don't use else if statement. Its probably finding the first match

#11
61Hiko
0
Frags
+
EseemedRes1180 [#10]

I think its a problem with your if else statement try using only if statments don't use else if statement. Its probably finding the first match

only if gives me the ability to move diagonally, which I don't want in my 2D game. I already tested that

#12
61Hiko
0
Frags
+
noSananoLife [#4]

Try deepseek but use a Outlook email to create a account because a gmail wont work. it works better than chatgpt to solve complicated coding problems, just activate Deepthink

yeah this worked after asking couple times, since at first its change didn't completely solve it. Got it now though

#13
Benetheburrito
0
Frags
+

Probably a if else statement problem, I imagine that you check for W first in the code before A S and D so W is default. I don't know what your intended behaviour is tho. Like if a player pressed all W and A at the same time what do you want to happen?

#14
irenic_
0
Frags
+
EseemedRes1180 [#10]

I think its a problem with your if else statement try using only if statments don't use else if statement. Its probably finding the first match

you are right, idk why they're not listening to u

#15
OmenEnthusiast
0
Frags
+

Seeing the code would be useful, as I'm not sure why one of the keys would override the others, If you checkout https://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyListener.html it includes an example that you can run (also found at https://docs.oracle.com/javase/tutorial/uiswing/examples/events/index.html#KeyEventDemo) that shows the info received from the keylistener, and even in the official example this problem does not exist.

EDIT: if you are using keyReleased at any point to stop movement you may be creating a state issue in your game that causes one direction to be overridden until a keyReleased event occurs.

#16
noSananoLife
1
Frags
+
61Hiko [#12]

yeah this worked after asking couple times, since at first its change didn't completely solve it. Got it now though

W china AI
KANGKANG my goat

  • Preview
  • Edit
› check that that your post follows the forum rules and guidelines or get formatting help
Sign up or log in to post a comment