在地图上绘制精灵,而不是在屏幕上 [英] Draw a sprite onto the map not to the screen

查看:116
本文介绍了在地图上绘制精灵,而不是在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libgdx,我有一个平铺的地图,我想在上面绘制精灵.但是,子画面会绘制到实际的窗口上,因此当我移动相机时,子画面会停留在同一位置.我想让精灵在地图上移动.

I'm using libgdx and I have a tiled map which i want to draw the sprite onto. However the sprite is drawn onto the actual window so when I move the camera, the sprite stays in the same place. ?I want to the sprite to move on the map.

这是我当前渲染对象的方式

This is how i currently render my objects

    @Override
    public void render(float delta) {
        translateCamera();

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();

        renderer.setView(camera);

        renderer.render(bgLayers);
        batch.begin();

        batch.draw(splayerSprite, Gdx.graphics.getWidth() / 2,
                Gdx.graphics.getHeight() / 2);

        batch.end();
        renderer.render(fgLayers);

    }

它总是最终位于屏幕中间,但是我希望能够像使用(W,A,S,D)的照相机那样单独移动它们,并使用方向键移动我的播放器.然后,如果我想让相机锁定在播放器上,否则将其锁定在播放器上.

It always end up being in the middle of the screen, however I want to be able to move them seperatly like for example the camera with (W,A,S,D) and move my player with the direction keys. Then if I want the camera locks onto the player but other wise its free.

我是libgdx的新手,所以请多多包涵,谢谢

I'm new to libgdx so please bear with me, Thanks

推荐答案

问题是

The problem is the SpriteBatch projection matrix isn't being set to the Camera projection matrix. This means the Sprite is not being rendered relative to the Camera. This is why the camera is moving, but the sprite is not; the correct matrix is not being used.

此外,始终在屏幕宽度的一半和屏幕高度的一半处渲染精灵.要解决此问题,请调用

Also the sprite is being rendered always at half the screen's width, and half the screen's height. To fix this Call sprite.draw. This will use the Sprite's internal position.

通过SpriteBatch投影矩阵. gdx.math.Matrix4%29"rel =" noreferrer> batch.setProjectionMatrix(camera.combined).这将导致精灵相对于摄影机渲染.

Set the SpriteBatch projection matrix via batch.setProjectionMatrix(camera.combined). This will cause the sprite to be rendered relative to the camera.

@Override
public void render(float delta) {
    translateCamera();

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();

    renderer.setView(camera);

    renderer.render(bgLayers);

    //here's the line that was missing.
    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    //be sure to call this instead of specifying position yourself!
    splayerSprite.draw(batch);
    batch.end();
    renderer.render(fgLayers);

}

每按一次WASD,您仍然需要将相机的位置捕捉​​到精灵的位置,但这很简单.

You'll still need to handle snapping the camera's position to the sprite's position whenever WASD is pressed, but that's trivial.

//snap the camera to the sprite's center.
if(wasd_isDown){
    float centerX = sprite.getX()+sprite.getWidth()/2;
    float centerY = sprite.getY()+sprite.getHeight()/2;
    camera.position.set(x,y, 0);
}

如果按下方向键,只需通过

If direction keys are pressed, just translate the camera's position vector via Vector3.add like so:

if(!wasd_isDown){
    float deltaX = 0;
    float deltaY = 0;
    float MOVE_DIST = 10;//or whatever you need.

    if(leftPressed) deltaX = -MOVE_DIST;
    else if(rightPressed) deltaX = MOVE_DIST;

    if(upPressed)deltaY = MOVE_DIST;
    else if(downPressed)deltaY = -MOVE_DIST;

    camera.position.add(deltaX, deltaY, 0);
}

这将使摄像机仅在播放器使用方向键时才独立移动,并允许相对于摄像机的方向渲染精灵.按下WASD时,它还将立即将相机捕捉回到精灵.

This will allow the camera to move independently only when the player uses directional keys, and will allow the sprite be be rendered in relation to the camera's orientation. It will also snap the camera immediately back to the sprite when WASD is pressed.

这篇关于在地图上绘制精灵,而不是在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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