在我的游戏里有一个坦克。我得到了一个桶类,它扩展了Sprite这个类处理和桶相关的东西,其中之一就是拍摄时的爆炸动画。我就是这么做的:

batch.draw(currShotAnimation,
            b2body.getPosition().x - currShotAnimation.getRegionWidth() / 2,
            b2body.getPosition().y + 10,
            0,
            0,
            currShotAnimation.getRegionWidth(),
            currShotAnimation.getRegionHeight(),
            1,
            1,
            getRotation());

当坦克向前看时,它正在工作:
java - Libgdx从枪中爆炸-LMLPHP
但当他看向不同的地方时,会觉得很奇怪:
java - Libgdx从枪中爆炸-LMLPHP
java - Libgdx从枪中爆炸-LMLPHP
我怎样才能把它修好,使它能向四面八方工作呢?
编辑:
在尝试了Abhishek Aryan建议后(我要测量的像素是1):
        batch.draw(currShotAnimation,
            b2body.getPosition().x + offset * MathUtils.cosDeg(getRotation()),
            b2body.getPosition().y + offset * MathUtils.sinDeg(getRotation()),
            currShotAnimation.getRegionWidth()/2f,
            currShotAnimation.getRegionHeight()/2f,
            currShotAnimation.getRegionWidth(),
            currShotAnimation.getRegionHeight(),
            1,
            1,
            getRotation());

看起来是这样的:
java - Libgdx从枪中爆炸-LMLPHP
java - Libgdx从枪中爆炸-LMLPHP

最佳答案

b2body是你的box2d身体吗如果是,则将仪表转换为像素,以便在屏幕上绘制。

private float offset=10;
private float METER_PIXEL=32;

batch.draw(textureRegion,b2body.getPosition().x*METER_PIXEL+ offset *MathUtils.cosDeg(getRotation()),b2body.getPosition().y*METER_PIXEL+ offset*MathUtils.sinDeg(getRotation()),currShotAnimation.getRegionWidth()/2f,currShotAnimation.getRegionHeight()/2f,currShotAnimation.getRegionWidth(),currShotAnimation.getRegionHeight(),1,1,getRotation());

这里你使用精灵旋转来旋转你的火焰动画。所以你需要根据b2dbody旋转来旋转你的精灵,这样你就可以在这里使用了,否则你需要得到b2body的弧度旋转,转换成度,在这里使用。
在这里我测试了一个纹理区域的偏移量
 public class TestGame extends Game implements InputProcessor{

    private SpriteBatch spriteBatch;
    private Sprite sprite;
    private TextureRegion textureRegion;
    private float offset = 20;
    private firstTex,secondTex;

    @Override
    public void create() {

        spriteBatch=new SpriteBatch();

        textureRegion=new TextureRegion(firstTex=new Texture("im.png"));
        sprite=new Sprite(secondTex=new Texture("xyz.png"));
        sprite.setPosition(100,100);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {

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

        spriteBatch.begin();
        sprite.draw(spriteBatch);
        spriteBatch.draw(textureRegion,sprite.getX()+ offset *MathUtils.cosDeg(sprite.getRotation()),sprite.getY()+(offset)*MathUtils.sinDeg(sprite.getRotation()),sprite.getWidth()/2f,sprite.getHeight()/2f,sprite.getWidth(),sprite.getHeight(),1,1,sprite.getRotation());
        spriteBatch.end();

        if(Gdx.input.isTouched()){
            float rotation=sprite.getRotation();
            rotation++;
            sprite.setRotation(rotation);
        }

    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        float rotation=sprite.getRotation();
        rotation++;
        sprite.setRotation(rotation);

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    @Override
    public void dispose(){
       firstTex.dispose();
       secondTex.dispose();
       spriteBatch.dispose();
   }
}

10-08 18:28