Jump to content
  • Advertisement
Sign in to follow this  
magorian

[java] Applet + KeyListener = Headache

This topic is 3088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, it has been a while since I did any Java programming, so I thought I'd have a play around with applets this morning. I have my applet set out like this:

I have a Game class that is in theory going to be where I have all my game code. This class implements KeyListener, MouseListener and MouseMotionListener.

I have my main class which extends java.applet.Applet. When a key is pressed (and handled by my this class using processKeyEvent) the applet goes from 'title screen' to 'game' mode, and registers its Game class (which is a member) as a key/mouse/mouse motion listener.

My problem here is that my Game class never receives any events, even though I have registered it as a listener for each type of event it is supposed to receive. I have search the forum and Google, but all the stuff I can find only shows the class that extends java.applet.Applet as implementing the listener interfaces, and all the replies involve registering itself as the listener, or adding requestFocus calls around the place. This is not helping!!! Does anyone have any idea what I am doing wrong?

A very simplified version of my code:

class OtherClass implements KeyListener {
private boolean mAKeyIsPressed = false;

public void keyPressed(KeyEvent pEvent) {
mAKeyIsPressed = true;
}

public void keyReleased(KeyEvent pEvent) {
mAKeyIsPressed = false;
}

public void keyTyped(KeyEvent pEvent) {
}

public boolean doUpdate() {
if(mAKeyIsPressed) {
return false;
}
return true;
}
}

class TheApplet extends java.applet.Applet implements Runnable{
private final int MODE_TITLE = 0;
private final int MODE_GAME = 1;
private OtherClass mMyOtherClass = new OtherClass();
private int mMode;

public void init() {
Thread t = new Thread(this);
t.start();
}

public void run() {
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
mMode = MODE_TITLE;

while(true) {
switch(mMode) {
case MODE_TITLE:
// update title blah blah
break;

case MODE_GAME:
if(!mMyOtherClass.doUpdate()) {
removeKeyListener(mMyOtherClass);
mMode = MODE_TITLE;
}
break;
}

repaint();
}
}

public void paint(Graphics pGraphics) {
// do paint
}

public void processKeyEvent(KeyEvent pEvent) {
if(mMode == MODE_TITLE) {
mMode = MODE_GAME;
addKeyListener(mMyOtherClass);
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Try adding the line


else{
super.processKeyEvent(pEvent);
}


to the end of your processKeyEvent.

Share this post


Link to post
Share on other sites
You're not supposed to override the event processing methods if you use an event listener. Furthermore, the event processing methods automatically dispatch events to your listeners, so you only have to register each listener once and you should not be registering a listener (or doing anything at all besides handling an event) in one of those methods. The solution is to move the addKeyListener() to an initialization method, and stop overriding the processKeyEvent method.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!