四时宝库

程序员的知识宝库

FXGL引擎序列帧与图片组件使用创建动画对象和汽车对象-----FXGL

#以书之名#

在FXGL引擎中,使用图片和序列帧来创建动画是游戏开发中的常见做法。通过图片组件和动画组件,可以为游戏对象添加生动的动画效果。本篇文章将介绍如何使用FXGL引擎中的TextureAnimatedTextureAnimationChannel等组件,创建汽车对象和爆炸动画对象。

1.基本框架与设置

首先,我们创建一个游戏应用类ImageGame,这个类是FXGL的主入口,负责游戏的初始化和设置。在initGame()方法中,我们通过spawn方法生成游戏中的实体(比如汽车和爆炸效果)。

package com.alatus.game;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.SpawnData;

import static com.almasb.fxgl.dsl.FXGL.*;

public class ImageGame extends GameApplication {

    @Override
    protected void initSettings(GameSettings gameSettings) {
        // 可以在这里设置游戏的一些配置项
    }

    @Override
    protected void initGame() {
        // 向游戏世界中添加实体工厂
        getGameWorld().addEntityFactory(new ImageEntityFactory());
        
        // 生成"Car"和"Boom"实体
        spawn("Car", new SpawnData(100, 100));  // 生成汽车实体
        spawn("Boom", new SpawnData(200, 200)); // 生成爆炸动画实体
    }

    public static void main(String[] args) {
        launch(args); // 启动游戏应用
    }
}

initGame()方法中,我们调用spawn()来创建“Car”汽车实体和“Boom”爆炸实体。这些实体的具体创建逻辑是通过ImageEntityFactory类实现的。

2.创建汽车对象

汽车实体的创建涉及到图片的加载、翻转和碰撞体积的设置。在FXGL中,Texture对象表示一张图片,而BoundingShape用来定义实体的碰撞区域。

package com.alatus.game;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.texture.Texture;

public class ImageEntityFactory implements EntityFactory {
    
    // 生成汽车实体
    @Spawns("Car")
    public Entity newCar(SpawnData data) {
        // 加载左侧的汽车图片,并缩放大小
        Texture carLeft = FXGL.texture("car.png", 32, 128);
        
        // 创建右侧的汽车图片,做水平镜像翻转
        Texture carRight = carLeft.copy();
        carRight.setScaleX(-1);
        carRight.setTranslateX(carLeft.getWidth());
        
        // 创建并返回一个汽车实体
        return FXGL.entityBuilder(data)
                .view(carLeft)      // 设置左侧视图
                .view(carRight)     // 设置右侧视图
                .bbox(BoundingShape.box(carLeft.getWidth() * 2, carLeft.getHeight()))  // 设置碰撞盒
                .build(); // 创建实体
    }
}

在这里,我们首先加载了汽车的图片car.png,并将其缩放到合适的大小。然后,通过copy()方法生成右侧的汽车图片,并使用setScaleX(-1)进行水平镜像翻转,以实现汽车的左右翻转效果。最后,我们为汽车实体设置了一个宽度为两倍车宽的碰撞盒。

3.创建爆炸动画对象

爆炸效果是通过序列帧动画来实现的。我们使用AnimationChannel来定义动画帧,并通过AnimatedTexture播放动画。ExpireCleanComponent用于指定动画的生命周期,动画结束后自动清除实体。

package com.alatus.game;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.texture.Texture;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import javafx.util.Duration;

public class ImageEntityFactory implements EntityFactory {
    
    // 生成爆炸动画实体
    @Spawns("Boom")
    public Entity newBoom(SpawnData data) {
        // 创建一个动画通道,定义图片、动画时长和帧数
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Boom.png"),
                Duration.seconds(2), 13); // 2秒钟完成13帧的动画
        
        // 创建动画纹理,并开始播放
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.play();
        
        // 创建并返回爆炸实体
        return FXGL.entityBuilder(data)
                .view(animatedTexture)  // 设置动画纹理作为视图
                .with(new ExpireCleanComponent(Duration.seconds(2.5)))  // 设置动画结束后清除实体
                .build();  // 创建实体
    }
}

在这段代码中,AnimationChannel定义了从Boom.png图像中提取的13帧动画,动画的总时长为2秒。通过AnimatedTexture,我们可以播放这些动画帧。当动画播放完成后,实体会在2.5秒后被自动移除。

4.总结

通过FXGL引擎,我们能够非常方便地使用图片和序列帧动画来创建动态的游戏对象。本文介绍了如何创建汽车对象,利用翻转效果实现汽车左右行驶的视觉效果;同时,我们也演示了如何使用序列帧动画来创建爆炸效果。通过这些组件,我们能够创建更加生动、具有表现力的游戏效果。

完整代码示例

package com.alatus.game;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.SpawnData;

import static com.almasb.fxgl.dsl.FXGL.*;

public class ImageGame extends GameApplication {

    @Override
    protected void initSettings(GameSettings gameSettings) {
        // 设置游戏相关配置
    }

    @Override
    protected void initGame() {
        // 向游戏世界添加实体工厂
        getGameWorld().addEntityFactory(new ImageEntityFactory());
        
        // 生成汽车和爆炸动画实体
        spawn("Car", new SpawnData(100, 100)); // 生成汽车
        spawn("Boom", new SpawnData(200, 200)); // 生成爆炸动画
    }

    public static void main(String[] args) {
        launch(args);  // 启动游戏
    }
}
package com.alatus.game;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.texture.Texture;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import javafx.util.Duration;

public class ImageEntityFactory implements EntityFactory {

    @Spawns("Car")
    public Entity newCar(SpawnData data) {
        Texture carLeft = FXGL.texture("car.png", 32, 128);
        Texture carRight = carLeft.copy();
        carRight.setScaleX(-1);
        carRight.setTranslateX(carLeft.getWidth());

        return FXGL.entityBuilder(data)
                .view(carLeft)
                .view(carRight)
                .bbox(BoundingShape.box(carLeft.getWidth() * 2, carLeft.getHeight()))
                .build();
    }

    @Spawns("Boom")
    public Entity newBoom(SpawnData data) {
        AnimationChannel animationChannel = new AnimationChannel(FXGL.image("Boom.png"),
                Duration.seconds(2), 13);
        AnimatedTexture animatedTexture = new AnimatedTexture(animationChannel);
        animatedTexture.play();

        return FXGL.entityBuilder(data)
                .view(animatedTexture)
                .with(new ExpireCleanComponent(Duration.seconds(2.5)))
                .build();
    }
}

创建包含动态动画和图像效果的汽车对象和爆炸动画。FXGL引擎为这些功能提供了简单而高效的实现方式。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言