JavaFX2.0-游戏精灵动画的一种方法

JavaFX2.0-游戏精灵动画的一种方法,java,swing,javafx-2,Java,Swing,Javafx 2,哇。。。在游戏编程方面,JavaFX2让我陷入了困境。我试图确定如何最好地接近玩家控制的精灵动画。(例如,制造敌人和事物,让玩家充满活力) 我知道如何编写代码来读取精灵表,并在AS3或Java swing中设置游戏循环。。。但我很难理解游戏中的动画循环应该如何与渲染的FX组件交互 我研究过api。有一个TranslateTransition类。但与其他语言相比,它似乎太过分了。eberything else看起来完全是基于接口的,或者只是太有限了 我正在读Weaver的Pro JavaFx2。。

哇。。。在游戏编程方面,JavaFX2让我陷入了困境。我试图确定如何最好地接近玩家控制的精灵动画。(例如,制造敌人和事物,让玩家充满活力)

我知道如何编写代码来读取精灵表,并在AS3或Java swing中设置游戏循环。。。但我很难理解游戏中的动画循环应该如何与渲染的FX组件交互

我研究过api。有一个TranslateTransition类。但与其他语言相比,它似乎太过分了。eberything else看起来完全是基于接口的,或者只是太有限了

我正在读Weaver的Pro JavaFx2。。。老兄,我在复制这种编码风格方面遇到了麻烦。但我能读到:)我还不知道是爱还是恨这只野兽

有什么建议吗?

阅读以下博客:


这是JavaFX sprite动画有史以来最好的实现:)

我参加这次聚会有点晚,但我想我会用JavaFX为sprite动画贡献我的解决方案。这里有一个类可以粘贴到您自己的项目中,以使用ImageView显示精灵动画,该动画与帧速率无关

import javafx.animation.AnimationTimer;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class ImageViewSprite extends AnimationTimer {


    private final ImageView imageView; //Image view that will display our sprite

    private final int totalFrames; //Total number of frames in the sequence
    private final float fps; //frames per second I.E. 24

    private final int cols; //Number of columns on the sprite sheet
    private final int rows; //Number of rows on the sprite sheet

    private final int frameWidth; //Width of an individual frame
    private final int frameHeight; //Height of an individual frame

    private int currentCol = 0;
    private int currentRow = 0;

    private long lastFrame = 0;

    public ImageViewSprite(ImageView imageView, Image image, int columns, int rows, int totalFrames, int frameWidth, int frameHeight, float framesPerSecond) {
        this.imageView = imageView;
        imageView.setImage(image);
        imageView.setViewport(new Rectangle2D(0, 0, frameWidth, frameHeight));

        cols = columns;
        this.rows = rows;
        this.totalFrames = totalFrames;
        this.frameWidth = frameWidth;
        this.frameHeight = frameHeight;
        fps = framesPerSecond;

        lastFrame = System.nanoTime();
    }

    @Override
    public void handle(long now) {
        int frameJump = (int) Math.floor((now - lastFrame) / (1000000000 / fps)); //Determine how many frames we need to advance to maintain frame rate independence

        //Do a bunch of math to determine where the viewport needs to be positioned on the sprite sheet
        if (frameJump >= 1) {
            lastFrame = now;
            int addRows = (int) Math.floor((float) frameJump / (float) cols);
            int frameAdd = frameJump - (addRows * cols);

            if (currentCol + frameAdd >= cols) {
                currentRow += addRows + 1;
                currentCol = frameAdd - (cols - currentCol);
            } else {
                currentRow += addRows;
                currentCol += frameAdd;
            }
            currentRow = (currentRow >= rows) ? currentRow - ((int) Math.floor((float) currentRow / rows) * rows) : currentRow;

            //The last row may or may not contain the full number of columns
            if ((currentRow * cols) + currentCol >= totalFrames) {
                currentRow = 0;
                currentCol = Math.abs(currentCol - (totalFrames - (int) (Math.floor((float) totalFrames / cols) * cols)));
            }

            imageView.setViewport(new Rectangle2D(currentCol * frameWidth, currentRow * frameHeight, frameWidth, frameHeight));
        }
    }
}
通过以下措施实现这一点:

ImageViewSprite anim = new ImageViewSprite(ImageView, new Image("/pathToImage"), columns, rows, totalNumberOfFrames, FrameWidth, FrameHeight, FPS);
anim.start();