【问题标题】:Specify Animation sprites size in world units ALL AT ONCE以世界单位指定动画精灵大小 ALL AT ONCE
【发布时间】:2014-10-24 12:39:54
【问题描述】:

如果我使用精灵,我可以参考我的游戏世界单位设置精灵的大小和位置:

    AtlasRegion region = textureAtlas.findRegion("0001");
    sprite = new Sprite(region);
    sprite.setPosition(11.5f, 5f);
    sprite.setSize(7f, 7f * region.getRegionHeight() / region.getRegionWidth());

如何通过动画实现这一点。如何设置每个关键帧精灵的大小?

textureAtlas = new TextureAtlas(Gdx.files.internal("data/bycicle.atlas"));

TextureRegion[] rotateUpFrames = new TextureRegion[4];

// Create an array of TextureRegions
rotateUpFrames[0] = (textureAtlas.findRegion("0001"));
rotateUpFrames[1] = (textureAtlas.findRegion("0002"));
rotateUpFrames[2] = (textureAtlas.findRegion("0003"));
rotateUpFrames[3] = (textureAtlas.findRegion("0004"));

rotateUpAnimation = new Animation(0.1f,rotateUpFrames);

【问题讨论】:

    标签: java libgdx game-engine


    【解决方案1】:

    我设置了一个动画子类,这样我就可以在每次使用新区域更新精灵时使用便捷的方法来应用缩放。例如:

    public class AnimationPlus extends Animation {
    
        float scale;
    
        public AnimationPlus (float frameDuration, 
                Array<? extends TextureRegion> keyFrames, PlayMode playMode, float scale) 
        {
            super(frameDuration, keyFrames, playMode);
            this.scale = scale;
        }
    
        public void applyKeyFrameToSprite (float stateTime, Sprite sprite)
        {
            TextureRegion region = getKeyFrame(stateTime);
            sprite.setSize(scale, 
                scale * region.getRegionHeight() / region.getRegionWidth());
        }
    }
    

    顺便说一句,如果你用下划线数字后缀来命名你的动画帧(例如“bike_0”、“bike_1”、“bike_2”等),那么你可以很方便地得到这样的区域数组:

    Array<TextureRegion> bikeRegions = textureAtlas.findRegions("bike");
    AnimationPlus animation = new AnimationPlus(0.1f, bikeRegions, PlayMode.LOOP, 7f);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多