【问题标题】:libgdx and android application. Image as backgroundlibgdx 和安卓应用程序。图像作为背景
【发布时间】:2012-02-21 20:28:49
【问题描述】:

我是新手,刚开始使用 libgdx。我想知道如何在我的游戏中将分辨率为 960x640 的 image.png 作为背景?这个有可能?感谢您的建议和宽容。也许你有一个简单的教程? 这是我的渲染类:

public void render() {

     texture = new Texture(Gdx.files.internal("E:/background.png"));

     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     batch.begin();
     batch.draw(texture, 0, 0);
     batch.end();
}

第二个问题。我需要插入两个活动图像,活动意味着当我单击该图像时,下一个图像会显示在屏幕上。我想在点击该图片时执行操作。

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    在您的 create() 方法中,创建一个引用 image.png 的新纹理,然后使用现有的 SpriteBatch 在 render() 循环中渲染它。在您调用 GL.clear() 之后,立即执行 batch.draw(backgroundTexture, 0. 0) 并确保您的相机处于 OrthographicProjection 模式。

    【讨论】:

    • 我得到错误:纹理宽度和高度必须是二的幂:960x640
    • 您可以通过在 Texture 类中重写 2 check 的力量来解决这个问题。在主 create() 方法的开头,添加:Texture.setEnforcePotImages(false);
    • 我没有看到任何性能或兼容性影响。我怀疑在具有仅支持 OpenGL 1.0 的较旧 GPU 的较旧设备上,这可能是一个问题。我不完全确定,我还没有在它不起作用的设备上测试它......
    【解决方案2】:

    首先你必须设置视口 在您的创建方法中执行此操作

    `float scrw = 960; 浮点数 = 640;

        camera = new OrthographicCamera();
        camera.viewportHeight = scrh;
        camera.viewportWidth = scrw;
    
        camera.position.set(camera.viewportWidth * .5f,
                camera.viewportHeight * .5f, 0f);
        camera.update();`
    

    创建纹理

    texture = new Texture("data/background.png");

    把这个纹理放在像这样的精灵中

    sprite=new sprite(texture);

    然后像这样设置大小

    sprite.setsize(960,640);

    并在你的渲染方法中在 batch.begin 之间绘制它 和batch.end

    sprite.draw(batch);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多