【问题标题】:libgdx Background TextureRegion repeatlibgdx 背景纹理区域重复
【发布时间】:2016-12-30 06:45:22
【问题描述】:

我是 libdgx 开发和游戏开发的新手。我正在阅读 Andreas Oehlke 的《Learning Libgdx Game Development》一书,并且我正在尝试并行开发自己的游戏。 我尝试添加背景时遇到问题。在书中,他使用了一种颜色,所以很简单。但我想从纹理图集中添加图像。图像太小无法恢复所有屏幕,所以我想重复一遍。我不能使用 regBackground.setWrap(TextureWrap.Repeat, TextureWrap.Repeat) 因为 regBackground 不是纹理。如何正确解决我的问题?

   public class Background extends AbstractGameObject {

   private TextureRegion regBackground;

   public Background () {
      init();
   }

   private void init () {
      dimension.set(1f, 1f);
      regBackground = Assets.instance.levelDecoration.background;
   }

   public void render (SpriteBatch batch) {
      TextureRegion reg = null;
      reg = regBackground;
      batch.draw(reg.getTexture(), 
            position.x, position.y, 
            origin.x, origin.y, 
            dimension.x, dimension.y, 
            scale.x, scale.y,
            rotation, 
            reg.getRegionX(), reg.getRegionY(), 
            reg.getRegionWidth(), reg.getRegionHeight(), 
            false, false);
   }
}

在我的 Assets 类中,我有这段代码可以在纹理图集中找到该区域:

     public class AssetLevelDecoration {
         public final AtlasRegion background;

         public AssetLevelDecoration (TextureAtlas atlas) {
             background = atlas.findRegion("background");
      }
   }

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    我在解决问题方面取得了进展。我使用 setWrap 方法来重复我的纹理:

       public class Background extends AbstractGameObject {
    
       private TextureRegion regBackground;
    
       public Background (int width, int heigth) {
          init(width, heigth);
       }
    
       private void init (int width, int heigth) {
          dimension.set(width, heigth);
          regBackground = Assets.instance.levelDecoration.background;
    
          origin.x = -dimension.x/2;
          origin.y = -dimension.y/2;
       }
    
       public void render (SpriteBatch batch) {
          TextureRegion reg = null;
          reg = regBackground;
    
          Texture test = reg.getTexture();
          test.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
          batch.draw(test, 
                position.x + origin.x, position.y + origin.y, 
                test.getWidth(), test.getHeight(), 
                reg.getRegionX(), reg.getRegionY(), 
                reg.getRegionWidth(), reg.getRegionHeight()       
                );
    
       }
    }
    

    现在,我得到了这个,但我只想重复我的背景图像(木方)。

    http://s24.postimg.org/c1m92ffwx/Capture_du_2013_11_12_15_49_03.jpg

    问题在于 getTexture() 恢复了所有图像,而不仅仅是我的背景。我该如何解决这个问题?

    【讨论】:

      【解决方案2】:

      我只想添加评论,但我没有代表。

      要解决整个纹理的重复问题,而不仅仅是木制正方形,您有 2 种选择。 A)将纹理区域分离到单独的纹理上。 B)循环重复绘制,这在性能方面应该可以忽略不计。

      http://badlogicgames.com/forum/viewtopic.php?f=11&t=8883

      【讨论】:

        【解决方案3】:

        我在这里创建了包含重复纹理的图像介绍:https://libgdx.info/basic_image/

        希望对你有帮助

        这会沿着这条线创建一个图像:

        【讨论】:

          【解决方案4】:

          一个更简单的方法是

           batch.draw(imageReference,startX,startY,widthOfScreen,heightOfScreen);
          

          batch.draw() 是一个重载方法,所以只使用你需要的那些参数 语法只是一个伪代码

          最重要的是,这可能会导致图像拉伸(取决于图像)。

          【讨论】:

          • 能否说的更具体些。
          • batch.draw(reg.getTexture(),0,0,480,800); //我很高兴你的相机高度是 800,宽度是 480 //关于我的答案中你需要规范的部分先生
          • 你的代码给了我一个黑屏。如果我不使用 reg.getRegionX()、reg.getRegionY()、reg.getRegionWidth() 和 reg.getRegionHeight(),则不会显示图像。我不想拉伸我想重复的图像。我想我需要使用 TiledDrawable 但我不知道如何使用它。
          • 你也可以写一个for循环画图
          • 您正在绘制背景或对象,您需要为其设置边界和其他内容
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-10
          • 1970-01-01
          相关资源
          最近更新 更多