如何为自上而下的游戏扩展LibGdx中的背景? [英] How to extend the background in LibGdx for top down game?

查看:80
本文介绍了如何为自上而下的游戏扩展LibGdx中的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自上而下的游戏中扩展背景的正确方法是什么?我使用了LibGdx框架.关于自上而下游戏的任何想法或教程.我的背景是PNG格式和720x1280肖像的屏幕.我在扩展背景时遇到问题.我希望相机跟随角色并扩展背景.我该怎么办?这是

What is the proper way to extend the background in top down game? I used LibGdx framework. Any idea or tutorial for top down game.My background is in PNG format and screen of 720x1280 portrait.I had a problem in extending the background.I want the camera follow the character and the background will extend. How could I do that? Here is the Screen shot of

https://i.stack.imgur.com/jl03R.png

这是我的代码

为了显示背景,我使用了这个

To display background I used this

    //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;

背景正在滚动,但相机未跟随播放器(猫)

The background is scrolling but the camera don't follow the player(cat)

GameScreen的源代码 http://pastebin.com/Dxfx9f65

Source code for GameScreen http://pastebin.com/Dxfx9f65

非常感谢您的帮助和建议. :)

Thank's and Advance any help or suggestion are much appreciated. :)

推荐答案

使用两个相同的纹理背景.每个屏幕的大小都可以是相同的文件.垂直对接很重要.同时移动.彼此交替变化. 示例代码:

Use two identical texture background. Each the size of the screen It can be the same file. It is important that are docked vertically. Move of at the same time. Alternately changing with each other. Sample code:

声明:

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格式-占用的内存更少.

The background can be made in the format jpg - will spend less memory.

这篇关于如何为自上而下的游戏扩展LibGdx中的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆