【问题标题】:LibGDX - How do I add a background ImageLibGDX - 如何添加背景图像
【发布时间】:2014-10-17 13:29:45
【问题描述】:

我刚刚开始学习 LibGDX,而且学习速度非常慢。我查看了许多关于如何向游戏添加背景图像的教程,但对于像我这样的初学者来说,它们中的大多数已经过时或太难了。下面是我试图让图像显示的内容。

    texture = new Texture(Gdx.files.internal("background.jpg"));
    backgroundTexture = new TextureRegion(texture, 0, 0, 2048, 563)

不幸的是,它没有用。我运行了应用程序,屏幕仍然是黑色的。

就像我说的,我是新手,所以请尽可能简单地解释一下。

【问题讨论】:

  • 请提供有关它如何不起作用的更多详细信息。它不编译吗?你能运行应用程序吗?你在屏幕上看到什么了吗?您是否在日志中收到异常消息? (如果您收到异常消息,请附上。)
  • 我运行了应用程序,但屏幕仍然像往常一样黑,他们什么都不是。
  • 你需要使用render()函数中的SpriteBatch来绘制纹理区域。我建议您在第一次创建项目时查看 LibGDX 为您生成的初始代码 - 它显示一个图像。

标签: java android libgdx


【解决方案1】:

您正在创建纹理,但您从未使用过它。访问here 以获取有关如何在屏幕上实际绘制事物的更多信息。如果这令人困惑,我建议在进一步使用该库之前先查找有关 java 的教程

【讨论】:

    【解决方案2】:

    您只为您的texture 字段分配了一张图片,然后将其放入TextureRegion 字段中。因为这发生在SpriteBatch.Begin()SpriteBatch.End() 中,所以你实际上并没有用它来绘图。

    在您的构造函数或create 方法中,您应该拥有您所做的:

    public void create() {
        //...
        backgroundTexture = new TextureRegion(new Texture("background.jpg"), 0, 0, 2048, 563);
        //Those are weird screen sizes btw (2048/563)
        //...
    }
    

    在你的render 方法中你应该有这个:

    public void render() {
        //...
        batch.Begin();
        batch.draw(backgroundTexture, 0, 0);
        //I believe texture region takes the upper left corner as 0,0 and batch.Draw the bottom left.
        //So you might need to do something like this:
        batch.draw(backgroundTexture, 0, Gdx.graphics.getHeight());
        batch.End();
        //...
    }
    

    这至少应该为你画出来。如果您的相机没有指向正确的坐标,那么显然您将看不到它或仅看到它的一部分。但是,如果您没有摆弄batch.setProjectionMatrix,您应该会很好。

    声明:我只是在文档的帮助下在这里写了一些东西,所以它可能不会立即起作用。

    【讨论】:

    • 有没有办法在调整纹理/背景大小时始终保持纵横比?
    【解决方案3】:

    在渲染方法上绘制

    spritebatch.begin();

    spritebatch.draw(texture, 0,0, width_of _screen, height_of_screen);

    spritebatch.end();

    【讨论】:

      【解决方案4】:
      package <<here your package name>>;
      
      import com.badlogic.gdx.ApplicationAdapter;
      import com.badlogic.gdx.Gdx;
      import com.badlogic.gdx.graphics.GL20;
      import com.badlogic.gdx.graphics.Texture;
      import com.badlogic.gdx.graphics.g2d.Sprite;
      import com.badlogic.gdx.graphics.g2d.SpriteBatch;
      
      
      public class MyGdxGame extends ApplicationAdapter {
          SpriteBatch batch;
          Texture img;
          @Override
          public void create() {
          batch = new SpriteBatch();
          img = new Texture("background.png");//name of your file.type
          
          }
      
          @Override
          public void render() {
              Gdx.gl.glClearColor(1,0,0,1);//the color of the backgrond if your image small than the windows
              Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
              batch.begin();
      
          
              batch.draw(img,0,0,1000,700);// image,x,y,wight,height
      
      //this one  for getting your image in center if it was icon or small image
              //batch.draw(img,Gdx.graphics.getWidth()/2 - img.getWidth()/2,Gdx.graphics.getHeight()/2 - img.getHeight()/2);
              //batch.draw(batch,text,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
              batch.end();
          }
      }
      
      

      【讨论】:

      • 您能否在您的代码 sn-p 之外添加文本来解释这段代码的作用?
      • 此代码使用 bach.draw 将图像作为您的 libgdx 游戏的背景,并带有所需或您想要的参数
      【解决方案5】:

      我将使它变得简单: 您所做的是从 background.png 创建了一个名为“texture”的纹理 现在这只是意味着您将纹理加载到内存中。但是你想把它画在屏幕上。所以在你的 show() 方法中创建一个 Spritebatch。 Spritebatch 是一种帮助您在屏幕上绘制东西的工具。

      private SpriteBatch batch;
      
      @Override
      public void show () {
          batch = new SpriteBatch();
      }
      

      然后你必须使用 SpriteBatch 在屏幕上绘制背景。

       @Override
      public void render(float delta) {
          batch.draw(texture, 0, 0, 200, 200);
      }          //  texture, X, Y, width, height
                     
      

      现在这将在屏幕上为您绘制纹理。但它不会填满你的屏幕。为了填满你的整个屏幕,我们必须让它和屏幕一样大。 您可以使用

      获取屏幕高度
      Gdx.graphics.getHeight()
      

      宽度与

      Gdx.graphics.getWidth()
      

      【讨论】:

        【解决方案6】:

        为了让它重复使用 for 语句,我的背景长度为 700,宽度为 500,我有一个 10 屏幕大(19200 x 10800 像素)的游戏,

        for(int PositionX = 0; PositionX < 19200; PositionX += 700) {
            for(int PositionY = 0; PositionY < 10800; PositionY += 500) {
                SpriteBatch.draw(Background, PositionX, PositionY);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-27
          • 2021-05-14
          • 1970-01-01
          • 2018-08-14
          • 2019-02-28
          • 2016-08-19
          • 2018-04-18
          • 1970-01-01
          相关资源
          最近更新 更多