相关疑难解决方法(0)

LibGdx如何使用OrthographicCamera滚动?

在此输入图像描述

我已经忙了10个小时(字面意思)而且我已经完成了,我需要问一下.我正在学习如何使用LibGdx编写Java游戏.我正在做一个水平太空船游戏.所以,我最糟糕的问题是我不知道如何滚动(我认为绘图会更好地解释).我想绘制一个hudge背景(Space)并使我的OrthographicCamera像我的SpaceShip一样向右移动,因此它将创建一个带有Space Background的Scroll效果.没有敌人,只有屏幕上的船只.

我在嘲笑这个

 public void moveCamera(float x,float y){
    cam.position.set(x, y, 0);
 }
Run Code Online (Sandbox Code Playgroud)

然后我在我的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();
}
Run Code Online (Sandbox Code Playgroud)

我实际上移动了相机的位置(我可以看到这要归功于println),但它并没有在游戏中移动,所以SpaceShip只是在窗口的边缘消失了.

我也在spriteBatch.end()之前尝试过这个

spriteBatch.setProjectionMatrix(camera.combined);
Run Code Online (Sandbox Code Playgroud)

但当我这样做时,窗户显示黑屏,没有船,没有什么.正如我所说,我很绝望,我看到很多例子(用鼠标滚动,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());     
}
Run Code Online (Sandbox Code Playgroud)

如果有人想在硬核模式下提供帮助,您可以在这里下载代码

我的代码(不工作)

我终于解决了它!WIIIIIIIIII!这是我在类名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();
} …
Run Code Online (Sandbox Code Playgroud)

java scroll libgdx

11
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×1

libgdx ×1

scroll ×1