【问题标题】:How to dynamically combine multiple animated spritesheets in LibGDX?如何在 LibGDX 中动态组合多个动画精灵表?
【发布时间】:2017-12-03 21:29:20
【问题描述】:

我正在制作一个 2D libdgx 游戏,其中动画角色开始游戏时没有穿衣服。然后他在每张地图上找到衣服物品,然后捡起来,他应该穿上它们。基本上,在拿起一个物品后,我们应该看到角色身上的动画物品(如内衣)。我有不同的精灵表,每个服装项目一张。如何在不编写 100 多行代码为纹理区域和帧设置动画的情况下对它们进行分层?

【问题讨论】:

    标签: java animation libgdx 2d


    【解决方案1】:

    我假设每个动画都有自己的位置和大小。

    我建议以下解决方案:

    1) 扩展Animation 类,并在其中放入保存动画唯一参数的字段,例如:

    public class MyAnimation extends Animation<TextureRegion> {
        private float xOffset; //x relative to the hero
        private float yOffset; //y relative to the hero
        private float width;
        private float height;
        //constructors, getters, etc.
    }
    

    2) 将所有动画存储在某处,例如:

    ObjectMap<String, MyAnimation> animationsByNames = new ObjectMap<String, MyAnimation>();
    animationsByNames.put(
            "heroWithoutClothes",
            new MyAnimation(0, 0, heroWidth, heroHeight, frameDuration, heroKeyframes)
    );
    animationsByNames.put(
            "underwear",
            new MyAnimation(underwearOffsetX, underwearOffsetY,underwearWidth, underwearHeight, frameDuration, underwearKeyframes)
    );
    //etc...
    

    3) 将活动动画存储在单独的Array

    Array<MyAnimation> activeAnimations = new Array<MyAnimation>();
    //hero animation is probably always active
    activeAnimations.add(animationsByNames.get("heroWithoutClothes"));
    

    4)当英雄捡起衣服时,为该衣服添加一个动画到activeAnimations中:

    private void pickUpClothes(String clothesName) {
        activeAnimations.add(animationsByNames.get(clothesName));
    }
    

    5) 最后,在您的 render 方法中的某处:

    for (int i = 0, n = activeAnimations.size; i < n; i++) {
        MyAnimation animation = activeAnimations.get(i);
        float x = heroX + animation.getXOffset();
        float y = heroY + animation.getYOffset();
        //... retrieve all other parameters, and use them to draw the animation
        //animation stateTime and frameDuration are the same for all of the animations
    }
    

    【讨论】:

    • 嘿!感谢您的精心帮助。我最终使用 makeanimation 方法创建了一个新的 animationUtil 类,现在将尝试重构以使用数组和对象映射!
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多