GPWiki.org
GPWiki.org
It is currently Tue May 21, 2013 4:01 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Sep 05, 2011 9:15 am 
Novice
User avatar

Joined: Thu Sep 01, 2011 3:44 pm
Posts: 6
Hello!

I'd like to request a little bit help.
I've started to write a "Pong" clone as advised, and I have arrived to the first problem I can't figure out how to solve.

I made two paddles, and I'm planning to make possible the game play for 2 players using the same keyboard at the same time. The left paddle is moving if 'W' or 'S' keys are pressed, while the right paddle is moved by the 'Up Arrow' or the 'Down Arrow' keys.

My problem is, if I press 'W' or 'S', it moves the left paddle as supposed, but if I press one of the arrow keys at the same time the left paddle gets ignored and vice versa.

I've tried to separate the movements to different threads, but I still can't find the correct way.

I basically check if the KeyEvent.getKeyCode() returned in the keyPressed() method is equals to the keyCode of the keys listed and then call the movement method with the appropriate params.

I'd prefer if You could point out where I fail or explain it with pseudo code.

Thanks in advance,
zilik

p.s: I'm running ubuntu linux 10.10 "maverick"

_________________
The ultimate source of FREE programming tutorials.


Top
 Profile  
 
PostPosted: Mon Sep 05, 2011 9:52 am 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3735
Location: South Africa
If you're using if..else if statements, then only 1 of them is going to fire off (the first true one). You could have separate if statements for player 1 and 2 and then it wouldn't conflict.

The other possible problem could be that your keyboard just doesn't support pressing a lot of keys at the same time. There are some pretty funky problems that can crop up with this.

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Mon Sep 05, 2011 10:01 am 
Novice
User avatar

Joined: Thu Sep 01, 2011 3:44 pm
Posts: 6
I've used if; if; if; if; statements.

However, my I'm trying something other now:
I will use the keyPeressed() method to set boolean values (_KEY_DOWN) to true and keyReleased() to set them false, and I will handle the movement in my run() method according to those boolean values.

I have to change a few things, but I hope it will be better. :spin

_________________
The ultimate source of FREE programming tutorials.


Top
 Profile  
 
PostPosted: Mon Sep 05, 2011 10:33 am 
Novice
User avatar

Joined: Thu Sep 01, 2011 3:44 pm
Posts: 6
zilik wrote:
I'm trying something other
I hit two birds with one stone. It fixed my problem without using new threads and besides that, it made the paddle's movement much smoother. 8)

_________________
The ultimate source of FREE programming tutorials.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group