简体   繁体   English

有人可以告诉我为什么键绑定不起作用吗?

[英]Can someone please tell me why the key-bindings don't work?

ok so I'm new to java and I'm trying to learning making 2d games but I have done all the things the tutorial guy has done and the keybinds apparently work for him and not mine好的,所以我是 java 的新手,我正在尝试学习制作 2d 游戏,但我已经完成了教程人员所做的所有事情,而且键绑定显然对他有用,而不是我的
these are the files这些是文件

main主要的

import javax.swing.JFrame;

public class main {
    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setVisible(true);
        
        MyPanel panel = new MyPanel();
        window.add(panel);
        window.pack();
        
        window.setLocationRelativeTo(null);
        window.setTitle("2D Adventure");
        
        panel.startGameThread();
}
}

the game panel游戏面板

import java.awt.Color;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

@SuppressWarnings("serial")

public class MyPanel extends JPanel implements Runnable {
    final int originalTileSize = 16;
    final int scale = 3;

final int tileSize = originalTileSize * scale;
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenwidth = tileSize * maxScreenCol;
final int screenHeight = tileSize * maxScreenRow;
int FPS = 60;

keyHandler keyh = new keyHandler();
Thread gameThread;


int playerX = 100;
int playerY = 100;
int playerSpeed = 4;




public MyPanel() {
    this.setPreferredSize(new Dimension(screenwidth, screenHeight));
    this.setBackground(Color.black);
    this.setDoubleBuffered(true);
    this.addKeyListener(new keyHandler());
    this.setFocusable(true);}

public void startGameThread() {
    gameThread = new Thread(this);
    gameThread.start();
}

@Override
public void run() {
    //nanoseconds into fps
    double drawInterval = 1000000000/FPS;
    double nextDrawTime = System.nanoTime() + drawInterval;
    
    
    while (gameThread != null) {
        

        
        update();
        
        repaint();
        

        try {
            double remainingTime = nextDrawTime - drawInterval;
            remainingTime /= 1000000;
            Thread.sleep((long) remainingTime);
            
            if (remainingTime < 0) {
                remainingTime = 0;
            }
            
            nextDrawTime += drawInterval;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void update() {
    if (keyh.uPressed == true) {
        playerY -= playerSpeed;
        //added this print command to see if the keyHandlers work but apparently they don't
        System.out.println("" + true);
    }
    else if (keyh.lPressed == true) {
        playerX -= playerSpeed;
    }
    else if (keyh.dPressed == true) {
        playerY += playerSpeed;
    }
    else if (keyh.rPressed == true) {
        playerX += playerSpeed;
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setColor(Color.WHITE);
    g2.fillRect(playerX, playerY, tileSize, tileSize);
    
    //and here the player x and y are not changing
    System.out.println("" + playerX + playerY);
    g2.dispose();
}

} }

and the keyHandler和 keyHandler

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class keyHandler implements KeyListener {
    public boolean rPressed = false;
    public boolean dPressed = false;
    public boolean lPressed = false;
    public boolean uPressed = false;
    
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @SuppressWarnings("static-access")
    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        String key = e.getKeyText(code);
        switch (key) {
        case "W":
            uPressed = true;
        case "S":
            dPressed = true;
        case "A":
            lPressed = true;
        case "D":
            rPressed = true;
        }
        
    }

    @SuppressWarnings("static-access")
    @Override
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();
        String key = e.getKeyText(code);
        switch (key) {
        case "W":
            uPressed = false;
        case "S":
            dPressed = false;
        case "A":
            lPressed = false;
        case "D":
            rPressed = false;
        }
    }

}

sorry, it will be hard for you to figure out what all this code is but please respond soon.抱歉,您很难弄清楚所有这些代码是什么,但请尽快回复。 thanks谢谢

In MyPanel you are adding a new instance of the keyHandler (ie, this.addKeyListener(new keyHandler()); ) to your panel, instead of passing the one you instantiated out of the contructor.MyPanel中,您正在向面板添加一个新的 keyHandler 实例(即this.addKeyListener(new keyHandler()); ),而不是将您实例化的实例传递给构造函数。 So, you should have:所以,你应该有:

this.addKeyListener(keyh);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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