libgdx vector3转换错误

时间:2015-01-20 04:54:38

标签: android libgdx

晚上所有人,

我试图通过教程Here熟悉libdgx和android。一切看起来都很好,除了抓住屏幕坐标,因为它们在Vector3转换中变得歪斜。

因此101的x输入转换为-796,7输入的y输入转换为-429(触摸屏幕的左上角,模拟器的结果与我的手机相同)。单击右下角时,动画将在屏幕中间触发。

这一切似乎都非常基本,所以不确定我设置错误以获得偏斜的转换。任何帮助表示赞赏!

相机创作:

camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);

抓住触摸协调:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touchCoordinateX = screenX;
    touchCoordinateY = screenY;
    stateTime = 0;
    explosionHappening = true;
    return true;
}

渲染循环:

public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stateTime += Gdx.graphics.getDeltaTime();
    batch.begin();

    if (!explosionAnimation.isAnimationFinished(stateTime) && explosionHappening) {
        Vector3 touchPoint = new Vector3();
        touchPoint.set(touchCoordinateX,touchCoordinateY,0);
        TextureRegion currentFrame = explosionAnimation.getKeyFrame(stateTime, false);  // #16
        camera.unproject(touchPoint);
        batch.draw(currentFrame, touchPoint.x, touchPoint.y);
    }
            //  batch.draw(img, 0, 0);
    batch.end();
    if (explosionAnimation.isAnimationFinished(stateTime)){explosionHappening = false;}
}

1 个答案:

答案 0 :(得分:1)

我想您忘了将相机投影矩阵设置为SpriteBatch。只需添加

batch.setProjectionMatrix(camera.combined);

batch.begin();