gpt4 book ai didi

java - ActionListeners 的 Stackoverflow 错误

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

当我尝试使用箭头键控制我的角色时,一切正常,但过了一会儿我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError

然后

at java.awt.AWTEventMulticaster.keyPressed(AWTEventMulticaster.java:249)

多次。我拥有的类(class)甚至不到 249 行代码,所以我不知道在哪里寻找问题。我的代码现在非常困惑,但如果它能帮助任何人找到答案,我会发布它。

import javax.swing.JButton;
import javax.swing.JComponent;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.image.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class mainFrame extends JPanel implements Runnable, KeyListener {
static boolean gameIsRunning = false;
static final int TARGET_FPS = 1;
static int x = 100;
static int y = 100;
static long startTime = 0;
static long elapsedTime = 0;
static long waitTime = 0;
JFrame window = new JFrame("Out from Eden");
JPanel panel = new JPanel();
getResources get = new getResources();
BufferedImage player = get.getImg("player");
static int playerVal = 0;
static int i = 0;
static String currentState = "";
static int lastFacing = 0;

public void createFrame() {
gameIsRunning = true;
gameStart();
}

public void setGame(boolean game) {
gameIsRunning = game;
}

public void gameStart() {
(new Thread(new mainFrame())).start();
}

public void run() {
while (gameIsRunning == true) {
startTime = System.nanoTime();
updateGame();
renderGame();
elapsedTime = System.nanoTime() - startTime;
waitTime = (TARGET_FPS * 10) - (elapsedTime / 1000000);

if (waitTime < 0) {
waitTime = 5;
}
try {
Thread.sleep(waitTime);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void updateGame() {
if (x > 800) {
x = 0;
}

if (currentState == "left") {
x--;
}
if (currentState == "right") {
x++;
}
player = royImage();

}

public BufferedImage royImage() {
if (currentState == "right") {
lastFacing = 2;
i++;
if (playerVal == 0) {
playerVal = 1;
i = 0;
player = get.getImg("royWalk1");
}
if (playerVal == 1 && i > 20) {
playerVal = 2;
i = 0;
return get.getImg("royWalk2");
}
if (playerVal == 2 && i > 20) {
playerVal = 3;
i = 0;
player = get.getImg("royWalk3");
}
if (playerVal == 3 && i > 20) {
playerVal = 4;
i = 0;
player = get.getImg("royWalk4");
}
if (playerVal == 4 && i > 20) {
playerVal = 0;
i = 0;
player = get.getImg("royWalk1");
}
}

if (currentState == "left") {
lastFacing = 1;
i++;
if (playerVal == 0) {
playerVal = 1;
i = 0;
player = get.getImg("royWalkL1");
}
if (playerVal == 1 && i > 20) {
playerVal = 2;
i = 0;
player = get.getImg("royWalkL2");
}
if (playerVal == 2 && i > 20) {
playerVal = 3;
i = 0;
player = get.getImg("royWalkL3");
}
if (playerVal == 3 && i > 20) {
playerVal = 4;
i = 0;
player = get.getImg("royWalkL4");
}
if (playerVal == 4 && i > 20) {
playerVal = 0;
i = 0;
player = get.getImg("royWalkL1");
}
}

if (currentState == "null") {
i = 0;
playerVal = 0;
if (lastFacing == 1) {
player = get.getImg("playerL");
}
if (lastFacing == 2) {
player = get.getImg("player");
}
}
return player;
}

public void renderGame() {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 700, 500);
panel.setLayout(new BorderLayout());
window.getContentPane().add(panel);
panel.add(this);
window.setFocusable(true);
window.setFocusTraversalKeysEnabled(false);
window.setVisible(true);
window.addKeyListener(this);
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Hello", x, y);
g2.drawLine(0, 63, 700, 63);
g2.drawImage(player, x - 100, y - 100, null);
}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
currentState = "up";
}
if (code == KeyEvent.VK_DOWN) {
currentState = "down";
}
if (code == KeyEvent.VK_LEFT) {
currentState = "left";
}
if (code == KeyEvent.VK_RIGHT) {
currentState = "right";
}
}

public void keyReleased(KeyEvent e) {
currentState = "null";
}
}

谁能帮我弄清楚为什么会出现堆栈溢出错误以及如何避免它?

最佳答案

当您在该循环中无数次添加 KeyListener 时,您的 while true 循环会搬起石头砸自己的脚。不要那样做,尤其是你的 renderGame() 方法。为什么您甚至想要多次调用用于设置 GUI 的方法?调用该方法一次会更有意义。

此外,使用 Swing Timer(谷歌教程)代替 while 循环,避免使用 KeyListeners 以支持键绑定(bind)(谷歌教程,并查看 this example)并执行设置代码只有一次。

此外,您的代码有导致 Swing 线程错误的风险,因为您正在调用 Thread.sleep(...)while (true) - 后台线程。为了安全起见,请改用 Swing Timer。

此外,您在比较字符串时出错了。不要使用 ==!= 比较字符串。请改用 equals(...)equalsIgnoreCase(...) 方法。了解 == 检查两个 object references 是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的重点。所以不是

if (fu == "bar") {
// do something
}

做,

if ("bar".equals(fu)) {
// do something
}

或者,

if ("bar".equalsIgnoreCase(fu)) {
// do something
}

关于java - ActionListeners 的 Stackoverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722722/

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