gpt4 book ai didi

java - 关键监听器不工作

转载 作者:行者123 更新时间:2023-11-29 06:35:28 25 4
gpt4 key购买 nike

我一直在尝试创建一个 java 游戏,但我遇到了障碍,我无法让 java 听我的任何键,即使我只是使用 print 语句来测试它。据我了解,我已经正确实现了 KeyListener 并将 key 监听器添加到 Applet,但它仍然无法正常工作。

我的主课:

import java.awt.*;
import javax.swing.*;


public class Container extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;

public static Dimension size = new Dimension(720,560); //Size of Screen

private static final int PIXELSIZE = 2;
public static Dimension pixel = new Dimension(size.width/PIXELSIZE,
size.height/PIXELSIZE); // Dimesions of screen in terms of pixels


public static final String NAME = "Game";
public static boolean isRunning = false;
private Image screen;

public static Level level;
public static MainCharacter p1;

public Container(){
setPreferredSize(size);
addKeyListener(p1);
}

public void start(){
new Tile();
level = new Level();
p1 = new MainCharacter(20,40);

isRunning = true;
new Thread(this).start();
}

public void tick(){
p1.tick();
}

public void render(){
Graphics g = screen.getGraphics();
g.setColor(new Color(130,160,255));
g.fillRect(0, 0, pixel.width, pixel.height);
level.render(g);
p1.render(g);
g = getGraphics();
g.drawImage(screen, 0, 0, size.width, size.height,
0, 0, pixel.width, pixel.height, null);
g.dispose();
}

public void run() {
screen = createVolatileImage(pixel.width,pixel.height);
while(isRunning){
tick();
render();
try{
Thread.sleep(5);
}catch(InterruptedException e){}
}
}

public static void main(String[] args){
Container container = new Container();
JFrame frame = new JFrame();
frame.add(container);
frame.pack();
frame.setTitle(NAME);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
container.start();
}

public static void right(){
p1.right();
}

public static void left(){
p1.left();
}

}

我的性格类:

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

public class MainCharacter extends Tall implements KeyListener{
public double fallSpeed = 1.5;
public double moveSpeed = 1.0;
public double xSpeed = 1;


public MainCharacter(int width, int height){
setBounds(Container.pixel.width/2 - width/2,
Container.pixel.height/2 - height/2,
width, height);
}

public void tick(){
if(Container.level.space[(int)(x+width)][(int)(y+height)] &&
Container.level.space[(int)(x)][(int)(y+height)] &&
Container.level.space[(int)(x+width)][(int)(y)] &&
Container.level.space[(int)(x)][(int)(y)])
y += fallingSpeed;
}

public void render(Graphics g){
g.drawImage(Tile.tileset_terrain, (int)x, (int)y,
(int)(x+width),(int)(y+height),
Tile.CHARACTER[0]*Tile.TILE_SIZE,
Tile.CHARACTER[1]*Tile.TILE_SIZE,
Tile.CHARACTER[0]*Tile.TILE_SIZE +(int)width,
Tile.CHARACTER[1]*Tile.TILE_SIZE + (int)height, null);
}

public void right(){
x += xSpeed;
}

public void left(){
x -= xSpeed;
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("hey");

}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("hey");

}

@Override
public void keyTyped(KeyEvent e) {
System.out.println("hey");
}
}

最佳答案

当您将它添加为 KeyListener 时,它看起来像 p1 是 null。

在此处将其添加为 KeyListener:

public Container(){
setPreferredSize(size);
System.out.println(p1); // try this...
addKeyListener(p1);
}

但在这里实例化它:

public void start(){
new Tile();
level = new Level();
p1 = new MainCharacter(20,40);

isRunning = true;
new Thread(this).start();
}

关于java - 关键监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21714436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com