玩家精灵不会在Java 2D游戏中移动(重绘)

时间:2017-04-16 13:12:36

标签: java

我是Java新手,我正在努力制作一款简单的游戏。 我首先尝试将重绘放入paintComponent(),直到我尝试添加一些背景。 有没有人知道如何让我的玩家无论是否重新移动'重新制作'?

屏幕类

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

class Frame extends JFrame implements KeyListener
{
Map map;

public static void main(String[] args)
{
    Frame frame = new Frame();
    frame.addKeyListener(new Frame());
    frame.setVisible(true);
}

public Frame()
{
    setInit();
}

private void setInit()
{
    map = new Map();
    add(map);

    setSize(800, 800);//frame size 300 width and 300 height
    setTitle("Eerste Game");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setResizable(false);
    setLayout(new GridLayout(1,1,0,0));
    setFocusable(true);
    setVisible(true);
}

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    map.movePlayer(keyCode);
}

public void keyReleased(KeyEvent e)
{

}

public void keyTyped(KeyEvent e)
{

}
}  

地图类

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

class Map extends JPanel
{
   Player player;
   BufferedImage map;

    public Map() {
        player = new Player();
        try{map = ImageIO.read(new File("img/map.png"));}catch (Exception e){};
    }

    public void movePlayer(int keyPressed)
    {
        player.move(keyPressed);
        revalidate();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(player.img,player.getX(),player.getY(), null);
    }

}

玩家类

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

class Player
{
    BufferedImage img = null;
    private int x = 100;
    private int y = 100;

    public Player()
    {
        try{img = ImageIO.read(new File("img/bob.png"));}catch (Exception e){}
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public void move(int keyPressed)
    {
        if(keyPressed == KeyEvent.VK_W) {
            // handle up
            y = y - 5;
        }
        if (keyPressed == KeyEvent.VK_A) {
            // handle links
            x = x - 5;
        }
        if (keyPressed == KeyEvent.VK_S) {
            // handle onder
            y = y + 5;
        }
        if (keyPressed == KeyEvent.VK_D ) {
            // handle right
            x = x + 5;
        }

    }
}

2 个答案:

答案 0 :(得分:1)

您不必要地创建一个新的Frame对象作为KeyListener,因此KeyListener事件都转到另一个Frame实例,而不是显示的实例。变化

frame.addKeyListener(new Frame());

为:

frame.addKeyListener(frame);

现在,KeyListener驱动真正显示的感兴趣的Frame实例。

其他问题,这不好:

catch (Exception e){}

始终捕获特定的异常,并且几乎总是,您应该处理它们。至少做这样的事情:

catch (IOException e) {
    e.printStacktrace();
}

答案 1 :(得分:0)

你这样做:

frame.addKeyListener(new Frame());

这会创建一个新帧,而不是将当前帧添加为监听器。

所以你改变了它

 frame.addKeyListener(frame);