[英]LibGDX - How do I add a background Image
我刚刚开始学习 LibGDX,而且学得非常缓慢。 我已经查看了许多有关如何向游戏添加背景图像的教程,但对于像我这样的初学者来说,其中大多数已经过时或太难了。 以下是我试图让图像显示的内容。
texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 2048, 563)
不幸的是,它没有奏效。 我运行了该应用程序,但屏幕仍然是黑色和纯色的。
就像我说的,我是新手,所以请让你的解释尽可能简单。
您正在创建纹理,但您从未使用过它。 访问此处获取有关如何在屏幕上实际绘制内容的更多信息。 如果这会让我感到困惑,我建议在继续使用该库之前先查找有关 Java 的教程
你只为你的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
你应该很好。
免责声明:我只是在文档的帮助下在这里写了一些东西,所以它可能不会立即起作用。
在 render 方法上绘制
spritebatch.begin();
spritebatch.draw(texture, 0,0, width_of _screen, height_of_screen);
spritebatch.end();
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();
}
}
我将使它变得简单:您所做的是从 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()
为了使它重复使用 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.