libGDX沿着纹理移动正射相机

时间:2013-06-11 03:10:36

标签: java android libgdx

我一直在努力创造一个有人摔倒的游戏。我的背景纹理的大小是480x3200,我正在尝试创建游戏,以便相机在跌倒时将人保持在屏幕中间,然后停在底部。但我不能让我的背景延伸到它开始的屏幕之外,然后能够看到图像的其余部分。

我现在所做的所有代码都是将480x3200图像向下缩小以适应我当前的屏幕(我设置为480x800),然后随着人物的下降,背景不会改变。

这是我的WorldRenderer课程,我试图做不同的事情,但每次,当我开始向下移动时,我永远不会让人看到图像的不同部分。

    public WorldRenderer(SpriteBatch b, World w) {
    this.world = w;
    this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT); 
    this.cam.position.set(CAMERA_WIDTH / 2, Person.position.y,0);
    this.cam.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT);
    this.cam.update();
    spriteBatch = b;
    loadTextures();
}

public void render(float delta) { 
    person = world.getPerson();

    moveCamera(); 
    cam.update();
    spriteBatch.setProjectionMatrix(cam.combined);

    spriteBatch.disableBlending();
    spriteBatch.begin();
    renderBackground();
    spriteBatch.end();

    spriteBatch.enableBlending();
    spriteBatch.begin();
    renderObjects();
    spriteBatch.end();

}

private void moveCamera() {
    cam.position.set(cam.position.x, Person.position.y, 0); 
    cam.update();
}

private void renderObjects() {  
    renderPerson();
    renderBlocks();
    renderPlatforms();
}

private void renderBackground() {
    spriteBatch.draw(backgroundTexture, cam.position.x - CAMERA_WIDTH / 2, cam.position.y - CAMERA_HEIGHT / 2, CAMERA_WIDTH, CAMERA_HEIGHT);
}   
}

有人有任何建议吗?

编辑: 谢谢,我将renderBackground中的绘制更改为 spriteBatch.draw(backgroundTexture,0,0, CAMERA_WIDTH, CAMERA_HEIGHT * 4);它现在有效。

2 个答案:

答案 0 :(得分:1)

spriteBatch.draw(backgroundTexture, cam.position.x - CAMERA_WIDTH / 2,
    cam.position.y - CAMERA_HEIGHT / 2, CAMERA_WIDTH, CAMERA_HEIGHT);

此代码绘制相对于相机位置的背景图像。这就是改变相机位置对背景图像位置没有影响的原因。把它改成这样的东西:

spriteBatch.draw(backgroundTexture,0,0);

答案 1 :(得分:1)

或者你可以简单地使用ParrallaxBackground和ParrallaxLayer类

这样你就不必管理你的相机了

在上述课程中以优化的方式完成。

相关问题