【问题标题】:How can I change the texture in a Scene2D Image using libGDX?如何使用 libGDX 更改 Scene2D 图像中的纹理?
【发布时间】:2014-02-01 07:09:58
【问题描述】:

如何更改 Scene2D 图像中的纹理?

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Image.html

文档中没有这种方法。我通过为其构造函数提供纹理参考来创建我的。

【问题讨论】:

    标签: libgdx


    【解决方案1】:

    您必须更改可绘制对象并将新纹理包装在 SpriteDrawable

    Texture texture = ...;
    Image image = new Image(texture);
    
    // switch to a new texture
    Texture newTexture = ...;
    image.setDrawable(new SpriteDrawable(new Sprite(newTexture)));
    

    【讨论】:

    • 基于此,((SpriteDrawable)image.getDrawable()).getSprite().setTexture(newTexture); 是否也能正常工作?
    • 如果通过 Texture 构造函数创建 Image,那么它将不起作用,因为它将是 TextureRegionDrawable 而不是 SpriteDrawable。
    • 所以我应该改用你发布的方法?我只是想避免创建新对象,但我想这并不重要。
    • 嗯,这并不是要让人沮丧,但他们为什么不为Image 类创建一个.setTexture 函数。
    • 那么他们还需要添加一个setTextureRegion,一个setSprite,也许还有一个setPixmap,然后......你明白我的意思了。
    【解决方案2】:

    你可以使用:

    Texture texture = ...;
    Image image = new Image(texture);
    
    // change to a new texture
    Texture newTexture = ...;
    image.setDrawable(new TextureRegionDrawable(new TextureRegion(newTexture)));
    

    不使用 SpriteDrawable 或创建新的 Sprite 实例。

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 2015-06-24
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多