我已经找了 10 个小时(字面意思),我已经完成了,我需要问一下。事情是我正在学习如何使用 LibGdx 来编写 Java 游戏。我正在做一个水平太空飞船游戏。所以,我最糟糕的问题是我不知道如何滚动(我认为绘制会更好地解释)。我想绘制一个巨大的背景(太空),并使我的正交相机像太空船一样向右移动,这样它将在太空背景上创建滚动效果。没有敌人,除了屏幕上的船之外什么也没有。
我正在尝试这个:
public void moveCamera(float x,float y){
cam.position.set(x, y, 0);
}
然后我在 WorldRender render() 方法中使用该方法:
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x,ship.getPosition().y);
cam.update();
System.out.println(""+cam.position);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
我实际上移动了相机位置(多亏了 println,我可以看到这一点),但它在游戏中没有移动,所以 SpaceShip 只是消失在窗口边缘。
我也在 spriteBatch.end() 之前尝试过这个
spriteBatch.setProjectionMatrix(camera.combined);
但是当我这样做时,窗口只显示黑屏,没有船,什么也没有。
正如我所说,我很绝望,我看到很多例子(用鼠标滚动,paralexscrolling 等),但所有的都是高级的或者与我的代码无关。
这就是我画东西的方式。背景和船舶是 WorldRender 中的纹理。我将背景图像绘制得非常宽,所以我的目的是像我所说的那样进行一些滚动。这就是代码
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x*ppuX,ship.getPosition().y*ppuY, ship.WIDTH*ppuX,ship.HEIGHT*ppuY);
}
public void drawBackground(){
spriteBatch.draw(background, -10*ppuX,0*ppuY, Gdx.graphics.getWidth()*10,Gdx.graphics.getHeight());
}
如果有人想在硬核模式下提供帮助,您可以在这里下载代码
我的代码(不工作) http://ubuntuone.com/3IdlECzw1zdJnqfj7hKDd0
我终于解决了!
这是我在类 WorldRenderer 中使用的代码,它具有在 GameScreen 中调用的用于渲染、调整大小等的方法
public WorldRenderer(World world) {
// TODO Auto-generated constructor stub
this.world=world;
this.ship=world.getShip();
this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.position.set(ship.getPosition().x,CAMERA_HEIGHT/2,0);
this.cam.update();//actualizamos la camara
spriteBatch=new SpriteBatch();
loadTextures();
}
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y,10,10);
}
public void drawBackground(){
spriteBatch.draw(background, 0,0, 500,50);
}
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
public void moveCemara(float x){
cam.position.set(x+20,cam.position.y, 0);
cam.update();
}
在船内部我有这个方法,我在 WorldRenderer 的渲染中调用它来移动它
public void Move(float delta){
if(Gdx.input.isKeyPressed(Keys.LEFT)) this.position.x -=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.RIGHT)) this.position.x +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.UP)) this.position.y +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.DOWN)) this.position.y -=velocity *delta;
}
我还要非常感谢那些帮助过我的人。我将第一个答案标记为好的答案,但是,将两者混合才是真正的解决方案。
我在这里留下了一些我遵循的教程,这些教程对菜鸟来说非常好
这是一个很好的从头开始的教程LiGdx新手指南 http://bitowl.de/de/adventskalender/
一个简单的平台游戏平台游戏 http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/
一个非常简单的游戏桶游戏 http://code.google.com/p/libgdx/wiki/SimpleApp