在自上而下的游戏中扩展背景的正确方法是什么?我使用 LibGdx 框架。关于自上而下游戏的任何想法或教程。我的背景是 PNG 格式和 720x1280 肖像屏幕。我在扩展背景时遇到问题。我希望相机跟随角色并且背景会扩展。我怎么能这么做呢?这是
的屏幕截图这是我的代码
为了显示背景,我使用了这个
//background
Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
Background.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
bgsprite = new Sprite(Background);
渲染中
spriteBatch.draw(Background,0,100,0, srcy, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
srcy +=3;
背景在滚动,但相机不跟随玩家(猫)
GameScreen 的源代码 http://pastebin.com/Dxfx9f65
非常感谢并提前提供任何帮助或建议。 :)
最佳答案
使用两个相同的纹理背景。每个屏幕尺寸都可以是相同的文件。垂直对接非常重要。同时移动。互相交替变化。 示例代码:
声明:
Texture background1, background2;
SpriteBatch batch;
float yMax, yCoordBg1, yCoordBg2;
final int BACKGROUND_MOVE_SPEED = 100; // pixels per second. Put your value here.
创作:
Background1 = new Texture(Gdx.files.internal("floor.png"));
Background2 = new Texture(Gdx.files.internal("floor.png")); // identical
yMax = 1280;
yCoordBg1 = yMax*(-1); yCoordBg2 = 0;
在方法渲染中:
yCoordBg1 += BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime();
yCoordBg2 = yCoordbg1 + yMax; // We move the background, not the camera
if (yCoordBg1 >= 0) {
yCoordBg1 = yMax*(-1); yCoordBg2 = 0;
}
batch.begin();
batch.draw(background1, 0, yCoordBg1);
batch.draw(background2, 0, yCoordBg2);
batch.end();
背景可以采用 jpg 格式制作 - 将占用更少的内存。
关于java - 如何在 LibGdx 中扩展自顶向下游戏的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824168/