gpt4 book ai didi

带 keyListener 的 Java 键输入将不起作用。我不知道我写错了什么

转载 作者:行者123 更新时间:2023-12-01 12:08:20 25 4
gpt4 key购买 nike

我正在创建一个基本的 cookieclicker 游戏,并且我已经实现了一个键输入(空格键)。现在我遇到了一个无法解决的问题,并且 Eclipse 说在我运行它之前没有任何问题。

按键输入代码:

package Tryout;

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

public class Key_Input implements KeyListener {

private boolean[] keys = new boolean[10];

public boolean space;

public void update(){

space = keys[KeyEvent.VK_SPACE];

}

@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;

}

@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;

}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

}

然后是“主代码”:

    package Tryout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import Tryout.Key_Input;

public class adamTryout {

private static int width = 600;
private static int height = 338;

static JLabel textLabel = new JLabel();

static public Key_Input key;

public static void createWindow() {

JFrame frame = new JFrame("CookieClicker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);


textLabel = new JLabel("CookieClicker" ,SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(600, 338));

frame.getContentPane().add(textLabel, BorderLayout.CENTER);



frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);

}

public static void main(String[] args) {
// TODO Auto-generated method stub

createWindow();



double x = 0; //Number of Cookie Clickers

double z; // Cost of the Cookie Clickers

double cookies = 0; //Amount of Cookies

double count = 0; //Additional amount of cookies per second from the Cookie Clickers

final double constant = 0.1; //Amount of CPS (cookies per second) you get from 1 Cookie Clicker



while (1>0) { // the game loop, (infinity loop)
key.update(); // Code that can't run

if(key.space) { // Code that can't run
x++;
}

z = Math.ceil(15*java.lang.Math.pow(1.15, x));

if(cookies >= z) { //If you got enough cookies you autobuy a new CC and the number of CC's increase as well as the cost of a CC

textLabel.setText("You bought a new Cookie Clicker; +0.1 cookie each second!");

cookies = cookies - z;

x++;

try { //delayer (Uptades 1 time per second)
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}

}


try { //delayer (Uptades 1 time per second)
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}

count = x*constant;

cookies = cookies + 1 + count;

cookies = (double)Math.round(cookies*100)/100;

textLabel.setText("cookies; " + cookies + " \n " + "CPS; " + ((double)Math.round((count + 1)*100)/100) + " \n " + "Cookie Clickers; " + Math.round(x)); // Your current amount of cookies
}
}
}

JFrame 启动,但控制台输出是:

Exception in thread "main" java.lang.NullPointerException
at Tryout.adamTryout.main(adamTryout.java:59)

我根本不知道现在该怎么办。请帮忙。

最佳答案

首次在线使用变量键时未初始化

key.update();

实际上它根本没有初始化过......

关于带 keyListener 的 Java 键输入将不起作用。我不知道我写错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418678/

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