Hi, everyone. I’m searching for a way to have two players being able to control two separate characters from the same keyboard (awsd vs. left/up/right/down). I have searched google several times, and the only site that I found offered no useful information. I’ve also searched this forum, but couldn’t find anything helpful at all. On a forum, somebody said threads were the key, but did not elaborate. I would be thankful if someone can point me in the right direction.
You don’t need any extra threads. What you really need are configurable controls. How many keys and which keys can be pressed together is a hardware thing. Very old keyboards only allowed up to 2 or 3 key presses at the same time. Newer ones support more. However, some combinations simply won’t work because the “lanes” of the key matrix are re-used and therefore it’s unable to detect specific combinations.
Eg famous Cherry keyboard no go combos are:
up+left+space¹
left+up+right
a+s+x
a+s+z (or a+s+y with a qwertz layout)
a+s+w
a+s+q
[¹ That’s the reason why you shouldn’t use cursors+space. If you use space also allow ctrl.]
Alright, that’s interesting to know, thanks. I have more or less given up on a two player game, in favor of an AI opponent. I think I should also mention that I’m making this game as an application, not an applet. I think there are different ways that the two work, because I have seen applets where more than one key can be pressed at the same time (like player one is pressing Left while Two is pressing D, and both characters move correctly).
Specifically, I wanted a layout like A and D to move left and right. W is jump, and S is duck. Ctrl is punch, and Shift is kick. There’s a couple things I’m not sure of.
1.) When a directional key is pressed, there is a half second gap before smooth movement.
2.) If I’m moving right, and I press the button to jump, the character will land and stop moving, even if the right key is still being held.
That’s all I can think of. I’m not a computer science student (education major), so I can only program in my spare time. My skills atrophy a lot between major attempts at programming. I’m really thankful for this forum, and anyone who takes the time to answer and help.
If you’re going to write a game with two or more players on one keyboard, you need to read this. Basically, keyboards suck and as oNyx said, certain combinations will lock up and cause weird behaviour.
Other than that, all you really need to do is attach a keyboard listener to your canvas/frame and have it log key press and release states. That’ll all happen in your AWT event thread. Then just read the states in your main thread with your game loop. A traditional n00b error is trying to trigger movement off of the keyboard events (like key repeat) which is generally a bad idea because it causes that odd half-second pause before moving and ties your movement to your OS’s key repeat setting. For example code have a search around the forum.
I think there are different ways that the two work[…]
Nop, it’s pretty much the same thing.
Basically it’s done like this… you get the event, you change the value of some flag… then in your main loop you act accordingly to the value of that flag. It’s done like that everywhere.
Warning - while you were typing a new reply has been posted. You may wish to review your post.
bla… /me submits anyways
Ooh, could you expand on this a little bit? I’m working primarily off of a high school comp sci class’s knowledge, which is not much at all. Trying to learn off of code by my self is literally like learning a new language. I need someone to explain a couple things. I would be thankful if you could provide at least a small code example.
EDIT- Oh, and I’m not slacking or anything. Right now I’m looking through two or three different forums looking for exactly what I need.
I’m gonna go ahead and double post this, so that no one overlooks a small edit and wastes their time.
I have actually figured it out, thanks to someone’s code example, and a lot of squinting at the screen on my part.
I would like to thank the people who took their time to help me, you guys are great.
Oh, man!!! I’d been pulling my hair out thinking I was doing something wrong with up+left+space. THANKYOU!!!