1

我目前正在尝试学习一些有关编程游戏的知识。我得到了一个玩家类,扩展了我自己编写的精灵类。现在我想知道,如何在运行游戏的过程中更改玩家对象的图像。想象一艘宇宙飞船,当按下右箭头键时,它应该是一个不同的图像,向右倾斜。现在我正在尝试这样做:当按下按钮时(例如空格) setImage(新图像) 但是现在,当我调用此方法时,我的图像消失了,新的图像不会出现?

有任何想法吗?我的主类代码:

    public class Game extends JPanel implements Runnable{
    private static final long serialVersionUID = 1L;

    public boolean isRunning;

    public static final int WIDTH = 320;
    public static final int HEIGHT = 240;
    public static final int SCALE = 2;

    private Player player;

    public Game() {
        addKeyListener(new TAdapter());
        setFocusable(true);
        requestFocus();
        start();
    }

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

    public void stop() {
        isRunning = false;
    }

    public void run() {
        init();
        while(isRunning) {

            update();
            repaint();

            try {
                Thread.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void init() {
        player = new Player("/ship_blue", WIDTH - 32/2, 400);
    }

    public void update() {
        player.update();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;

        drawPlayer(g2d);
    }

    public void drawPlayer(Graphics2D g2d) {
        if(player.isVisible)g2d.drawImage(player.getImage(),(int) player.getX(), (int) player.getY(), null);
    }

    public static void main(String[] args) {
        Game gameComponent = new Game();
        Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);

        JFrame frame = new JFrame("Invaders");
        frame.setVisible(true);
        frame.setSize(size);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(gameComponent);
    }

    public class TAdapter extends KeyAdapter{
        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }

        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }
    }
}

这是主类,现在是我试图在 onPress() 方法中更改图像的播放器类:

    public class Player extends Sprite{

    private double glideSpeed = .125;
    private int fixY;
    private int xDirection = 0;
    private int yDirection = 1;

    public Player(String source, int x, int y) {
        this.x = x;
        this.y = y;
        ImageIcon ii = new ImageIcon(this.getClass().getResource(source));
        setImage(ii.getImage());
        setTileSize(ii.getIconWidth());
        setSpeed(0.2);
        fixY = y;
    }

    public void update() {
        x += xDirection * 1.4;
        glide(10);
    }

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

        if(key == KeyEvent.VK_RIGHT) {
            xDirection = 1;
        }
        if(key == KeyEvent.VK_LEFT) {
            xDirection = -1;
        }
        if(key == KeyEvent.VK_SPACE) {
            ImageIcon ii2 = new ImageIcon("/player_blue_negativ");
            setImage(ii2.getImage()); //<-----Here I am trying to change the image
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if(key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_LEFT) {
            xDirection = 0;
        }
    }

    public void glide(int span) {
        y += yDirection * glideSpeed;
        if(y - fixY > span || fixY - y > span) {
            yDirection = -yDirection;
        }
    }
}

并完成这里的一切是我的精灵类:

    public class Sprite {

    protected double x;
    protected double y;
    protected int tileSize;
    protected double speed;
    protected Image img;
    protected boolean isVisible;

    public Sprite() {
        isVisible = true;
    }

    public double getSpeed() {
        return speed;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public int getTileSize() {
        return tileSize;
    }

    public Image getImage() {
        return img;
    }

    public boolean getVisible() {
        return isVisible;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void setTileSize(int tileSize) {
        this.tileSize = tileSize;
    }

    public void setImage(Image img) {
        this.img = img;
    }

    public void die() {
        isVisible = false;
    }
}
4

2 回答 2

4
if(key == KeyEvent.VK_SPACE) {
    ImageIcon ii2 = new ImageIcon("/player_blue_negativ");

有一个基本的问题。所有图像都应在事件发生之前加载和缓存。当事件发生时,使用缓存的图像,GUI 应该立即呈现。

正如@camickr 所提到的,使用 anImageIcon也会巧妙地从 an滑到URLa String(假定代表 File路径)。坚持使用URLgetResource(..).

ImageObserver

如果使用 anImageObserver来绘制图像,观察者将被告知异步加载的图像的更新。所有组件(例如JPanel)实现ImageObserver,所以..

g2d.drawImage(player.getImage(),(int) player.getX(), (int) player.getY(), null);

应该:

g2d.drawImage(player.getImage(),(int) player.getX(), (int) player.getY(), this);

释放 EDT!

class Game extends JPanel implements Runnable ..

Thread.sleep(5);

不要阻塞 EDT(事件调度线程)——当这种情况发生时,GUI 将“冻结”。也不要尝试从 EDT 以外的任何东西更新 GUI。

代替调用Thread.sleep(n)a Runnable,实现一个 SwingTimer用于重复任务或 aSwingWorker用于长时间运行的任务。有关更多详细信息,请参阅Swing 中的并发。

用于paintComponent(Graphics)Swing 组件!

public void paint(Graphics g) {

应该:

@Override
public void paintComponent(Graphics g) {
于 2013-08-17T16:56:45.073 回答
2

加载初始图像时,您可以毫无问题地使用以下内容:

ImageIcon ii = new ImageIcon(this.getClass().getResource(source));

当您更改使用的图像时:

ImageIcon ii2 = new ImageIcon("/player_blue_negativ");

我猜你找不到图像,可能是因为你不需要“/”。但是为什么不使用您知道有效的相同方法呢?

于 2013-08-17T18:46:45.363 回答