Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualGetov

Posted 11 December 2012 - 06:08 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

#9Getov

Posted 11 December 2012 - 06:07 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

#8Getov

Posted 11 December 2012 - 06:05 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; case KeyEvent.VK_C: break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

#7Getov

Posted 11 December 2012 - 06:05 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; case KeyEvent.VK_C: break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

#6Getov

Posted 11 December 2012 - 06:05 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; case KeyEvent.VK_C: break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

#5Getov

Posted 11 December 2012 - 06:05 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; case KeyEvent.VK_C: break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

PARTNERS