我正在制作一个2D libdgx游戏,其中动画角色在没有衣服的情况下开始游戏。然后他在每张地图上找到衣服物品,然后把它们捡起来,他应该穿上它们。基本上,在拿起一件物品后,我们应该在角色上看到动画物品(如内衣)。我有不同的精灵表,每个服装项目一个。如何在不编写100多行代码的情况下对它们进行分层,以便为textureRegion和frames设置动画?
答案 0 :(得分:0)
我认为,每个动画都有自己的位置和大小。
我建议采用以下解决方案:
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
}