如何在JavaFX Animation中更改对象的填充? [英] How to change the fill of an object in a JavaFX Animation?

查看:78
本文介绍了如何在JavaFX Animation中更改对象的填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的Java电子游戏.我决定使用JavaFX的 Rectangle 类来塑造该视频游戏的角色.

I'm developing a simple Java videogame. I've decided to shape the characters of that videogame by using the Rectangle class of JavaFX.

当角色死亡时,我需要顺序更新纹理,直到角色死亡.因此,我需要实现动画.

When a character dies, I need to update the texture sequentially, until the death of the characters. I need to realize an animation for this reason.

我尝试使用 FillTransition ,但是我无法更改矩形的填充图像,而只能更改颜色...我应该使用哪种动画类?

I've tried to use the FillTransition, but I can't change the filling Image of the rectangle but only the color...what kind of animation class should I use?

一个角色的死亡由4个不同的精灵(四个png图像)表示,这些精灵显示角色逐渐躺在土壤上.在角色快要死的时候,我需要依次用这4张图像更改矩形的填充.

The death of a character is represented by 4 different sprites (four png images) which shows the character laying down to the soil gradually. I need to change the filling of the rectangle with these 4 images sequentially, while the character is dying.

推荐答案

矩形不是执行此类操作的好节点类型.为此,使用 ImageView 更为简单.

Rectangle is not a good node type to do this kind of stuff. It's much simpler to use ImageView for this purpose.

这允许您使用 Timeline 来操纵 ImageView image 属性:

This allows you do use a Timeline for manipulation of the image property of the ImageView:

final Image[] deathAnimationImages = new Image[] {...};

final ImageView character = ...;

Duration frameDuration = Duration.seconds(1d / deathAnimationImages.length); // 1 second for complete animation
Timeline deathAnimation = new Timeline(new KeyFrame(frameDuration, new EventHandler<ActionEvent>() {
    private int index = 0;    

    @Override
    public void handle(ActionEvent event) {
        character.setImage(deathAnimationImages[index]);
        index++;
    }
}));
deathAnimation.setCycleCount(deathAnimationImages.length);
deathAnimation.play();

如果使用 Rectangle + ImagePattern 来获取给定尺寸的图像,则可以使用 ImageView.fitWidth ImageView.fitHeight .

If you use Rectangle + ImagePattern to get a image of a given size you can produce the same result using ImageView.fitWidth and ImageView.fitHeight.

这篇关于如何在JavaFX Animation中更改对象的填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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