Java 如何为自顶向下的游戏扩展LibGdx中的背景?
在自上而下的游戏中,扩展背景的正确方法是什么?我使用了LibGdx框架。任何关于自上而下游戏的想法或教程。我的背景是PNG格式,屏幕为720x1280人像。我在扩展背景时遇到问题。我希望相机跟随角色,背景将扩展。我怎么能这么做?这是的屏幕截图 这是我的密码 为了显示背景,我使用了这个Java 如何为自顶向下的游戏扩展LibGdx中的背景?,java,android,libgdx,vertical-scrolling,top-down,Java,Android,Libgdx,Vertical Scrolling,Top Down,在自上而下的游戏中,扩展背景的正确方法是什么?我使用了LibGdx框架。任何关于自上而下游戏的想法或教程。我的背景是PNG格式,屏幕为720x1280人像。我在扩展背景时遇到问题。我希望相机跟随角色,背景将扩展。我怎么能这么做?这是的屏幕截图 这是我的密码 为了显示背景,我使用了这个 //background Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
//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;
背景在滚动,但相机不跟随播放器(cat)
GameScreen的源代码
感谢并提前提出任何帮助或建议,不胜感激。:) 使用两个相同的纹理背景。每个屏幕的大小可以是相同的文件。垂直停靠是很重要的。同时移动的。相互交替地改变。 示例代码: 声明:
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格式制作,这样会占用更少的内存。有了这个解决方案,我发现两个平铺之间有一个间隙。