4

うわー... ゲームのプログラミングに関しては、JavaFx 2 は私をループに陥れました。私は、プレーヤーが制御するスプライト アニメーションにどのようにアプローチするのが最善かを決定しようとしています。(つまり、敵や物を作り、プレイヤーを動かします)。

スプライトシートを読み取り、AS3 または Java スイングでゲーム ループを設定するコードの書き方は知っていますが、ゲームのアニメーション ループがコンポーネントと相互作用する方法について頭を悩ませています。 FXのレンダーです。

私はAPIを研究しました。TranslateTransition クラスがあります。しかし、他の言語に比べて過剰に思えます。それ以外は、完全にインターフェイス ベースであるか、あまりにも制限されているように見えます。

私はWeaverのPro JavaFx2を読んでいます...そして、そのコーディングスタイルを複製するのに苦労しています。しかし、私はそれを見つけることができます:) この獣を愛するか憎むかはまだわかりません.

助言がありますか?

4

2 に答える 2

5

このブログ エントリを読む: http://blog.netopyr.com/2012/03/09/creating-a-sprite-animation-with-javafx/

これは JavaFX スプライト アニメーションのこれまでで最高の実現です :)

于 2012-05-22T21:10:15.707 に答える
3

私はこのパーティーに少し遅れましたが、JavaFX を使用したスプライト アニメーションのソリューションに貢献したいと考えました。これは、独自のプロジェクトに貼り付けて、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();
于 2014-03-17T22:44:32.067 に答える