在我的游戏中动画瓷砖 [英] Animating Tiles In My Game

查看:34
本文介绍了在我的游戏中动画瓷砖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Pokemon Style 2D java 游戏,不使用任何库,只使用纯 Java,并且我正在努力让水瓷砖动画化,但遇到了问题.我希望磁贴每半秒左右更新一次.我将发布我的主要类、抽象瓷砖类、水类和屏幕类,以便您能想出一种方法,让我如何在我的游戏中为瓷砖制作动画.

I am making a Pokemon Style 2D java game using no libraries, just pure java, and I am working on and having issues getting a water tile to animate. I want the tile to update every half a second or so. I will post my main class, abstract tile class, water class, and screen class so that maybe you can figure out a way to so me how to animate tiles in my game.

P.S:现在我正在尝试为水瓷砖制作动画.并且所有的精灵都是为了测试,稍后会更改.

P.S: Right now I am trying to animate a water tile. And all the sprites are for testing and will be changed later.

DropBox 上的代码:AnimatedTileMain, Screen, Tile.

Code at DropBox: AnimatedTile, Main, Screen, Tile.

推荐答案

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

class AnimatedWater {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final JPanel gui = new JPanel(new GridLayout(2,0,0,0));

                final AnimatedTile[] tiles = new AnimatedTile[8];
                for (int ii=0; ii<tiles.length; ii++) {
                    tiles[ii] = new AnimatedTile();
                    gui.add(new JLabel(new ImageIcon(tiles[ii])));
                }
                ActionListener listener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        for (int ii=0; ii<tiles.length; ii++) {
                            tiles[ii].paintImage();
                            gui.repaint();
                        }
                    }
                };
                Timer timer = new Timer(50, listener);
                timer.start();

                JOptionPane.showMessageDialog(null, gui);
                timer.stop();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

class AnimatedTile extends BufferedImage {

    GradientPaint[] frameGradient;
    int frame = 0;

    AnimatedTile() {
        super(60,60,BufferedImage.TYPE_INT_RGB);
        frameGradient = new GradientPaint[6];
        for (int ii=0; ii<frameGradient.length; ii++) {
            frameGradient[ii] = new GradientPaint(
                    0f,(float)ii,Color.BLUE, 
                    0f,(float)ii+3,Color.CYAN,true);
        }
    }

    public void paintImage() {
        Graphics2D g = createGraphics();
        if (frame==frameGradient.length-1) frame = 0;
        else frame++;
        g.setPaint(frameGradient[frame]);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.dispose();
    }
}

这篇关于在我的游戏中动画瓷砖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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