如何在 LibGDX 中动态组合多个动画 spritesheet?
How to dynamically combine multiple animated spritesheets in LibGDX?
我正在制作一个 2D libdgx 游戏,其中一个动画角色开始游戏时没有穿衣服。然后他在每张地图上找到衣服物品,通过捡起它们,他应该穿上它们。基本上,在拿起一件物品后,我们应该会在角色身上看到该动画物品(如内衣)。我有不同的精灵表,每件衣服一张。如何在不编写 100 多行代码来对 textureRegion 和帧进行动画处理的情况下对它们进行分层?
我假设每个动画都有自己的位置和大小。
我建议以下解决方案:
1) 扩展 Animation
class,并在其中放置包含动画唯一参数的字段,例如:
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
}
我正在制作一个 2D libdgx 游戏,其中一个动画角色开始游戏时没有穿衣服。然后他在每张地图上找到衣服物品,通过捡起它们,他应该穿上它们。基本上,在拿起一件物品后,我们应该会在角色身上看到该动画物品(如内衣)。我有不同的精灵表,每件衣服一张。如何在不编写 100 多行代码来对 textureRegion 和帧进行动画处理的情况下对它们进行分层?
我假设每个动画都有自己的位置和大小。
我建议以下解决方案:
1) 扩展 Animation
class,并在其中放置包含动画唯一参数的字段,例如:
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
}