如何多次绘制相同的运动图像? [英] How do I draw the same moving image multiple times?

查看:24
本文介绍了如何多次绘制相同的运动图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在制作一个平台游戏,当你按下空格键时,角色会发射一个在屏幕上移动的火球,但是当你再次按下空格键时,火球的坐标将设置回玩家的坐标,而不是绘制另一个火球,这就是我想要.

Hello I'm making a platformer game and when you press space the character shoots a fireball that moves across the screen but when you press space again the fireball's coordinates are set back to the player's coordinates rather than drawing another fireball which is what I want.

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

public class Fire extends JPanel{
Image fireball;
private int x=155000,y=155000;
Player player = new Player();

public void paint(Graphics g){
    g.drawImage(fireball, x, y, null);
}

public Fire(){


}

public void update(){
    fireball = new ImageIcon("C:\Users\steven.greens10\Desktop\Programs\Raw        Java\Platform\res\fireball.png").getImage();
    x+=5;
    if(x > 640){
        x=155000;
    }
}

public void shoot(Player p){
    x = p.getX();
    y = p.getY();
    repaint();
}



}

推荐答案

@KevinWorkman 是对的.您需要某种数据结构来容纳火球.在下面的示例中,我使用了 FireballList.

@KevinWorkman is right. You need some kind of data structure to hold the fireballs. In the example below I used a List of Fireball.

List<Fireball> fireBalls;
...
private class Fireball {

    Image fireball;
    int x = 150;
    int y = 125;

    public Fireball(Image image) {
        fireball = image;
    }

    public void drawFireball(Graphics g) {
        g.drawImage(fireball, x, y, 50, 50, null);
    }
}

为了绘制它们,我只是遍历它们.为了让他们继续前进,我只是增加了计时器中的 x 值并调用了 repaint

To paint them, I just iterate through them. To make them move forward I just increas the x value in the timer and call repaint

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...
    for (Fireball ball : fireBalls) {
        ball.drawFireball(g);
    }
}

这是完整的代码

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.List;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.Timer;

public class WannaBeStreetFighter extends JPanel {

    private static final int D_W = 700;
    private static final int D_H = 250;
    private static final int X_INC = 10;

    List<Fireball> fireBalls;
    BufferedImage ryu;
    BufferedImage fireball;
    BufferedImage background;

    public WannaBeStreetFighter() {

        try {
            ryu = ImageIO.read(new URL("http://www.sirlin.net/storage/street_fighter/ryu_hadoken_pose.png?__SQUARESPACE_CACHEVERSION=1226531909576"));
            background = ImageIO.read(new URL("http://fightingstreet.com/folders/variousinfofolder/ehondasbath/hondasfz3stage.gif"));
            fireball = ImageIO.read(new URL("http://farm6.staticflickr.com/5480/12297371495_ec19ded155_o.png"));
        } catch (IOException ex) {
            Logger.getLogger(WannaBeStreetFighter.class.getName()).log(Level.SEVERE, null, ex);
        }

        fireBalls = new LinkedList<>();

        Timer timer = new Timer(40, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Iterator<Fireball> it = fireBalls.iterator();
                while (it.hasNext()) {
                    Fireball ball = it.next();
                    if (ball.x > D_W) {
                        it.remove();
                        System.out.println(fireBalls.size());
                    } else {
                        ball.x += X_INC;
                        repaint();
                    }
                }
            }
        });
        timer.start();

        InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(KeyStroke.getKeyStroke("SPACE"), "hadouken");
        getActionMap().put("hadouken", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireBalls.add(new Fireball(fireball));
            }
        });

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, D_W, D_H, this);
        g.drawImage(ryu, 50, 125, 150, 115, this);
        for (Fireball ball : fireBalls) {
            ball.drawFireball(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    private class Fireball {

        Image fireball;
        int x = 150;
        int y = 125;

        public Fireball(Image image) {
            fireball = image;
        }

        public void drawFireball(Graphics g) {
            g.drawImage(fireball, x, y, 75, 50, null);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Best Street Fighter ever");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new WannaBeStreetFighter());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

这篇关于如何多次绘制相同的运动图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆