【问题标题】:LibGDX - scaling animationsLibGDX - 缩放动画
【发布时间】:2015-01-25 17:53:26
【问题描述】:

又是一天,又是一个问题。

我正在尝试从 TextureRegions 制作动画,但我需要将其缩放一些值。我对缩放静止图像(由纹理区域制成的精灵)没有任何问题,但是我不知道为什么,它不适用于动画帧。

对于静止图像,我会这样做:

    darknessActive = new Sprite(AssetLoaderUI.darknessActive);
    darknessActive.setPosition(50, 20);
    darknessActive.setScale(scaler);

然后我在渲染器中渲染它就好了。

通过动画我尝试做这样的事情:

    Frame1 = new TextureRegion(texture, 764, 75, 141, -74);
    Frame2 = new TextureRegion(texture, 907, 75, 133, -75);

    Frame1S = new Sprite(Frame1);
    Frame1S.setScale(scaler);
    Frame2S = new Sprite(Frame2);
    Frame2S.setScale(scaler);

    Sprite[] Frames = { Frame1S, Frame2S };

    myAnimation = new Animation(0.06f, Frames);
    myAnimation.setPlayMode(Animation.PlayMode.LOOP);

但图像仍然是原始大小,“缩放器”没有任何区别。

【问题讨论】:

    标签: java animation libgdx sprite scaling


    【解决方案1】:

    2018 年编辑:在较新版本的 LibGDX 中,Animation 类不限于 TextureRegions。它可以为任何通用对象数组设置动画,因此您可以创建一个Animation<Sprite>,尽管您需要确保每次获得关键帧精灵时都应用了适当的比例。就个人而言,我认为应该避免使用 Sprite 类,因为它将资源(图像)与游戏状态混为一谈。


    你没有说你是如何绘制动画的,但大概你使用了一种不知道精灵比例的方法。

    由于Animation类存储了TextureRegions,如果你从动画中得到一帧来画成这样:

    spriteBatch.draw(animation.getKeyFrame(time), x, y);
    

    那么精灵批次不知道它是Sprite的实例,只知道它是TextureRegion的实例,不包含缩放信息。

    一种快速而肮脏的方法是这样的:

    Sprite sprite = (Sprite)animation.getKeyFrame(time));
    spriteBatch.draw(sprite, x, y, 0, 0, sprite.width, sprite.height, sprite.scaleX, sprite.scaleY, 0);
    

    但是,如果您想要一个行为类似于精灵的动画,您可以将其子类化以存储缩放数据(如果您愿意,还可以存储旋转和位置)并自行绘制。然后你就完全不用担心 Sprite 实例了。

    例如:

    public class SpriteAnimation extends Animation {
    
        float scaleX = 1;
        float scaleY = 1;
    
        /*...duplicate and call through to super constructors here...*/
    
        public void setScaling(float scale){
            scaleX = scale;
            scaleY = scale;
        }
    
        public void draw (float stateTime, Batch batch, float x, float y) {
            TextureRegion region = getKeyFrame(stateTime);
            batch.draw(region, x, y, region.width*scaleX, region.height*scaleY);
        }
    }
    

    您可以根据需要自定义它来存储 x 和 y、旋转、原点等,如果您愿意的话。创建它时,您根本不会使用 Sprite 类。你会做这样的事情。

    frame1 = new TextureRegion(texture, 764, 75, 141, -74);
    frame2 = new TextureRegion(texture, 907, 75, 133, -75);
    
    TextureRegion[] frames = { frame1, frame2 };
    
    mySpriteAnimation = new SpriteAnimation(0.06f, frames);
    mySpriteAnimation.setScaling(scaler);
    mySpriteAnimation.setPlayMode(Animation.PlayMode.LOOP);
    

    【讨论】:

    • 我不得不稍微改变绘制方法(创建 2 个新变量,newHeight 和 newWidth 并在那里缩放),但除此之外,这是一个非常简洁的解决方案,谢谢。
    • 这种方法的用法有什么不同吗?我尝试了batch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);,但与使用扩展类之前相比没有任何区别,仍然在屏幕上绘制巨大的精灵。是的,我正在以这种方式设置缩放和初始化 SpriteAnimation:animation = new SpriteAnimation(1/15f, atlas.getRegions()); animation.setScaling(0.01f);
    • @kacpr draw 方法的重载没有缩放参数。选择具有您需要 SpriteBatch 了解的参数的 draw 重载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2012-12-14
    • 2014-03-21
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多