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

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

问题描述

我不使用图书馆,只是纯java制作口袋妖怪风格的2D 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.

code。在投放箱: AnimatedTile 屏幕 瓷砖

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天全站免登陆