简体   繁体   中英

KeyListener is unresponsive 1st time used?

I looked at these links, none helped me. Im making a game with a JFrame using KeyListener and Runnalbe. I've never had a problem with it in games i have made in the past, but now heres what im talking about:

  • i start my program and the window comes up, message displays everything looks fine.
  • no keys work -i close the window -The 2nd time its opened everything works fine. -If i reset the virtual machine and try again, back to square 1.

here is my constructor:

public Game()
{
    super("ZMan");
    addKeyListener(this);
    setSize(800,600);
    setVisible(true);


    i = createImage(800,600);
    b = i.getGraphics();
    makeLevel();
    souls = new Image[3];
    particles = new double[20][3];
    pc = 10;
    pH = 20;
    player = new Image[8];
    nme = new Image[8];
    showMessage = false;
    try
    {
        bg = ImageIO.read(new File("bg.png"));
        abg = ImageIO.read(new File("abg.png"));
        bil = ImageIO.read(new File("buildingP.png"));
        bilP = ImageIO.read(new File("building.png"));
        message = ImageIO.read(new File("./system/messageBG.png"));

        souls[2] = ImageIO.read(new File("sR.png"));
        souls[1] = ImageIO.read(new File("sG.png"));
        souls[0] = ImageIO.read(new File("sB.png"));
        rem = souls[0];
        //player
        player[0] = ImageIO.read(new File("./player/normal.png"));
        player[1] = ImageIO.read(new File("./player/pBloop.png"));
        player[2] = ImageIO.read(new File("./player/pscorp.png"));
        player[3] = ImageIO.read(new File("./player/pSqui.png"));
        player[4] = ImageIO.read(new File("./player/pCato.png"));
        player[5] = ImageIO.read(new File("./player/pChubs.png"));
        player[6] = ImageIO.read(new File("./player/pPluckers.png"));
        player[7] = ImageIO.read(new File("./player/pSpooky.png"));
        gate = ImageIO.read(new File("./gate.png"));
        levelUp = ImageIO.read(new File("./system/levelUp.png"));
        xL = ImageIO.read(new File("./player/x.png"));
        /*
        nme[0] = ImageIO.read(new File("./enemies/bloop.png"));
        nme[1] = ImageIO.read(new File("./enemies/bloop.png"));
        nme[2] = ImageIO.read(new File("./enemies/scorp.png"));
        nme[3] = ImageIO.read(new File("./enemies/squi.png"));
        nme[4] = ImageIO.read(new File("./enemies/cato.png"));
         */
        nme[0] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
        nme[1] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
        nme[2] = Toolkit.getDefaultToolkit().createImage("./enemies/scorp.png");
        nme[3] = Toolkit.getDefaultToolkit().createImage("./enemies/squi.png");
        nme[4] = Toolkit.getDefaultToolkit().createImage("./enemies/cato.png");
        nme[5] = Toolkit.getDefaultToolkit().createImage("./enemies/chubs.png");
        nme[6] = Toolkit.getDefaultToolkit().createImage("./enemies/pluckers.png");
        nme[7] = Toolkit.getDefaultToolkit().createImage("./enemies/spooky.png");
    }
    catch(Exception e){e.printStackTrace();}
    powerT = new String[8];
    powerT[0] = "You went back to normal";
    powerT[1] = "You are now Bloop!";
    powerT[2] = "You are now Scorp!";
    powerT[3] = "You are now Squi!";
    powerT[4] = "You are now Cato!";
    powerT[5] = "You are now Chubs!";
    powerT[6] = "You are now Pluckers";
    powerT[7] = "You are now Spooky";
    startAim();
    //music = new Music("./system/music/0.wav");
    t.start();
}

does nyone know the problem?

"To fire keyboard events, a component must have the keyboard focus."— How to Write a Key Listener . I'm guessing the relevant component has focus the second time. You can try requestFocusInWindow() or, preferably, look at How to Use Key Bindings .

Addendum: There's a key binding example here .

This may sound dumb, but it's more of a sanity check. Check your main method. I'd recommend doing a debug stepthrough, making sure that the first time the window is launched, the constructor to Game is actually called...Nothing about the constructor appears to be bad (to me), which makes me think that the instantiation of the object might be hosed.

If it isn't that, I can't help you; you'll have to wait for someone smarter in the specifics of your problem to answer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM