java


Keylistener is “laggy”. Stops working then works again after about 5 seconds


Original Question:
I have a question about Keylisteners. I'm trying to make a simple 2D shooter game for my class. The problem doesn't seem to be in the code as the character's movement works fine, but keylistener suddenly stops working and then works again after 5 seconds. But during those 5 seconds, keylistener doesn't register anything. I've checked this by printing out my set of pressed keys and during the "lag time" nothing I press registers. How can I fix this? Below is my code for the game class and character movement.
Summary of What I Tried/Changed:
I have reformatted the block code as well as tried to put j1.addKeyListener(this) into main as user7185318 suggested. However, I encountered an error saying "cannot use this in a static context." I tried rephrasing the problem (below) to be more specific. To Lew Block, could you show me how to run GUI on EDT?
Rephrased Problem Description:
My character is a sprite BufferedImage that I drew. The BufferedImage's location is set to that of a Rectangle that is essentially a hit box for the character. I used the affineTransform class to allow the BufferedImage to spin. I noticed that the problem usually comes with repressing the movement keys. (W = up, S = down, etc...) The first time I press these keys, the character moves smoothly and spins as it is supposed to. However, after I let go of the keys and press the keys again, the character doesn't respond anymore. This failure of response doesn't happen every time. Sometimes it works, other times it doesn't.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
public class Game extends GameDriverV2 implements KeyListener{
//Background Rectangle
Rectangle bg = new Rectangle(0,0,1200,700);
//Main Player
Player p1 = new Player(this.addImage("character.png"));
Rectangle p1Follow = new Rectangle(100, 300, 55, 55);
double playerTurnAngle = 0.0;
boolean playerTurnR = false, playerTurnL = false;
int playerDx, playerDy;
int timer;
//set of pressed keys
private final Set<Character> pressed = new HashSet<Character>();
public Game(){
this.addKeyListener(this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame j1 = new JFrame();
j1.setSize(1200,700);
j1.setTitle("Game"); //Change when know the name of the game
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.add(new Game());
j1.setVisible(true);
}
#Override
public void draw(Graphics2D win){
//draw and set color for background
win.setColor(Color.BLACK);
win.fill(bg);
//setting change in angle by KeyListenerBelow
if(playerTurnR){
playerTurnAngle += Math.PI/45;
}else if(playerTurnL){
playerTurnAngle -= Math.PI/45;
}
p1.moveAndDrawNorm(win, p1Follow);
p1.setPlayerTurnAngle(playerTurnAngle);
p1Follow.translate(playerDx, playerDy);
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
pressed.add(e.getKeyChar());
if (pressed.size() > 1) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
playerTurnR = true;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
playerTurnL = true;
}
if(e.getKeyCode() == KeyEvent.VK_A){
playerDx = -3;
}
if(e.getKeyCode() == KeyEvent.VK_D){
playerDx = 3;
}
if(e.getKeyCode() == KeyEvent.VK_W){
playerDy = -3;
}
if(e.getKeyCode() == KeyEvent.VK_S){
playerDy = 3;
}
}else{
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
playerTurnR = true;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
playerTurnL = true;
}
if(e.getKeyCode() == KeyEvent.VK_A){
playerDx = -3;
}
if(e.getKeyCode() == KeyEvent.VK_D){
playerDx = 3;
}
if(e.getKeyCode() == KeyEvent.VK_W){
playerDy = -3;
}
if(e.getKeyCode() == KeyEvent.VK_S){
playerDy = 3;
}
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
pressed.remove(e.getKeyChar());
if (pressed.size() > 1) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
playerTurnR = false;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
playerTurnL = false;
}
if(e.getKeyCode() == KeyEvent.VK_A){
playerDx = 0;
}
if(e.getKeyCode() == KeyEvent.VK_D){
playerDx = 0;
}
if(e.getKeyCode() == KeyEvent.VK_W){
playerDy = 0;
}
if(e.getKeyCode() == KeyEvent.VK_S){
playerDy = 0;
}
}else{
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
playerTurnR = false;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
playerTurnL = false;
}
if(e.getKeyCode() == KeyEvent.VK_A){
playerDx = 0;
}
if(e.getKeyCode() == KeyEvent.VK_D){
playerDx = 0;
}
if(e.getKeyCode() == KeyEvent.VK_W){
playerDy = 0;
}
if(e.getKeyCode() == KeyEvent.VK_S){
playerDy = 0;
}
}
}
}

Related Links

Repeated replacement of characters in String
Changes to parameters passed to a static function in java [duplicate]
Replace duplicate java objects including all references
Iterating over two collections with two loops
App crashes when trying to show AlertDialog in thread
Converting a String Array to an Int array [closed]
Android User Session
LinkedBlockingQueue's thread-safety with many producers in a producer-consumer scenario
Getting final status for radio button
Concatenating a string variable to a url properly
Resize dialog message (JOptionPane) for long sentence with fixed width
struts2 interceptors and webservlets
Sintaxis JQPL Filter list
Formatting a date in JSF to be put into <p:tab title=“”> attribute
Remote debugging in eclipse issue
Selenium - how to get runtime generated iframe id

Categories

HOME
wso2-am
wso2
webpack
keycloak
gremlin
tinymce
gis
razor
elm
malloc
graphql
rdf
electronics
cross-browser
ezpublish
opengl-es-2.0
networkx
django-imagekit
mouse
worldwind
openrefine
serilog
aurigma
oxyplot
excel-vba-mac
contextmenu
django-cms
graphicsmagick
xpages-ssjs
javacv
cas
maquette
ejabberd-module
procdump
nouislider
jspresso
gtrendsr
stringtemplate
commit
hybridauth
subset-sum
mozilla
main
bitbucket-pipelines
serve
ensembles
http-live-streaming
eigenvalue
estimote
modelmapper
thin
apache-fop
theming
brightcove
composite-key
sqlbulkcopy
memory-alignment
lync-client-sdk
account-kit
root-framework
medium.com
wdf
static-ip-address
sts-springsourcetoolsuite
elements
fody
django-debug-toolbar
merge-conflict-resolution
actionbardrawertoggle
itextpdf
iis-arr
browser-bugs
splash
fluid-dynamics
generic-programming
offloading
qcodo
document-classification
android-radiobutton
tld
p4java
mbr
mcts
terminal-services
oam
jmapviewer
rabl
drools-planner
tinn-r
chuck
coderush
dice
frameset
javaspaces
spring-portlet-mvc
boost-filesystem
vdsp
subgurim-maps
horizontal-accordion
h.323
wise
getresponsestream
dbal
temporal-database
gacutil
lzh

Resources

Encrypt Message