尝试使用键盘和图像片的错误



我目前正在尝试为学校做一个迷宫游戏,我为何会遇到这些错误感到困惑。这是所涉及的代码。同样,已经进口所需的一切。首先是具有玩家位置的玩家类,然后是其图像。

public class Player {
int playerX;
int playerY;
int moveSpeed = 5; //I can edit move speed here
Image character;
//getters and setters to utilize the player's location and image
public Player(){ //constructor for initial starting points
    playerX = 50;
    playerY = 50;
    ImageIcon player = new ImageIcon("E://Workspace//Maze//images//Player.jpg");
    character = player.getImage();
}
public void setPlayerX(int playerX){
    this.playerX = playerX;
}
public int getPlayerX(){
    return playerX;
}
public void setPlayerY(int playerY){
    this.playerY = playerY;
}
public int getPlayerY(){
    return playerY;
}
public void setMoveSpeed(int moveSpeed){
    this.moveSpeed = moveSpeed;
}
public int getMoveSpeed(){
    return moveSpeed;
}
public Image getPlayerImage(){
    return character;
}
}

现在是创建JPANEL的布局类,应该是将玩家图像绘制到游戏中的最终原因,也应该允许玩家移动。

public class Layout extends JPanel implements ActionListener { //GUI with a      non null FlowLayout
Player p = new Player();
Maze m = new Maze();
//500 x 500 seemed like a good size for the maze game
int x = 500;
int y = 500;
public Layout(){
    setLayout(new FlowLayout());
    addKeyListener(new Move());
    setFocusable(true);
}
//for use in setting and getting the borders of the game
public void setX(int x){
    this.x = x;
}
public int getX(){
    return x;
}
public void setY(int y){
    this.y = y;
}
public int getY(){
    return y;
}
public void paintGame(Graphics g){
    super.paint(g);
    g.drawImage(p.getPlayerImage(), p.getPlayerX(), p.getPlayerY(), null);
}
@Overrride
public void actionPerformed(ActionEvent ae){
    repaint();
}
}
class Move extends Layout implements KeyListener { //Inheritance
public void keyPressed(KeyEvent press) { //for the movement in the game
    //I used both keys so that if the player woukld like to use WASD or the    arrow keys either will work
    if(press.getKeyCode() == KeyEvent.VK_W || press.getKeyCode() ==  KeyEvent.VK_UP){
        //move up
        p.setPlayerY(p.getPlayerY() - p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_S || press.getKeyCode() == KeyEvent.VK_DOWN){
        //move down
        p.setPlayerY(p.getPlayerY() + p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_A || press.getKeyCode() == KeyEvent.VK_LEFT){
        //move left
        p.setPlayerX(p.getPlayerX() - p.getMoveSpeed());
    }
    else if(press.getKeyCode() == KeyEvent.VK_D || press.getKeyCode() == KeyEvent.VK_RIGHT){
        //move right
        p.setPlayerX(p.getPlayerX() + p.getMoveSpeed());
    }
}

public void keyReleased(KeyEvent release) {
    //nothing is needed here
}
public void keyTyped(KeyEvent e) {
    //does nothing if a key is type (no need for it)
}
}

最后是应该运行游戏的游戏类(目前我只是想让玩家在空白空间中移动而没有实现地图)。

public class Play {
public static void main(String[]args) { 
    play();
}
public static void play(){
    Layout l = new Layout();
    Player p = new Player();
    Maze m = new Maze();
    l.setSize(l.getX(), l.getY()); //size can be changed in layout
    l.setVisible(true);
}
}

现在由于某种原因,当我尝试运行此问题时,我会遇到很多错误。我希望能尽快解决这个问题,以便我可以开始实现我的迷宫图。这是我遇到的错误:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.ref.SoftReference.get(Unknown Source)
at sun.misc.SoftCache$ValueCell.strip(Unknown Source)
at sun.misc.SoftCache$ValueCell.access$300(Unknown Source)
at sun.misc.SoftCache.get(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at mazeGame.Player.<init>(Player.java:16)
at mazeGame.Layout.<init>(Layout.java:11)
at mazeGame.Move.<init>(Layout.java:49)
at mazeGame.Layout.<init>(Layout.java:20)

at mazeGame.Move.<init>(Layout.java:49) at mazeGame.Layout.<init>(Layout.java:20)继续进行至少20行,然后Eclipse停止输出错误。请帮助我解决这个问题,因为我目前一直坚持如何使玩家移动,并且需要开始为迷宫创建地图。对不起,很长的帖子,但我想发布任何可能给我错误的东西。

问题是您的构造函数中有无限的递归。Layout()调用addKeyListener(new Move());Move扩展了Layout,因此Move的构造函数也调用Layout(),然后又调用Move()

您需要重构代码以避免这种情况。一种简单的方法是使移动不扩展布局,而是将播放器作为构造函数参数传递到Move

class Move implements KeyListener {
    final Player p;
    Move(Player p) {
        this.p = p;
    }
    ...
}

Layout

addKeyListener(new Move(p));

还考虑使用密钥绑定代替密钥侦听器。这通常是一种更好的方法。

尝试替换行:

ImageIcon player = new ImageIcon("E://Workspace//Maze//images//Player.jpg");
character = player.getImage();

with:

character = ImageIO.read(new File("E://Workspace//Maze//images//Player.jpg"));

我相信,通过使用MediaTracker监视图像的加载状态来预加载imageicon的异步加载可能会导致问题。api/javax/swing/imageicon.html

相关内容

  • 没有找到相关文章

最新更新