gpt4 book ai didi

java - 来自枪的 Libgdx 爆炸

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:15:13 26 4
gpt4 key购买 nike

在我的游戏中有一辆坦克。我有一个扩展 Sprite 的 Barrel 类,这个类负责处理与桶相关的东西,其中之一就是射击时的爆炸动画。这就是我尝试这样做的方式:

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

当坦克向前看时它正在工作:
top down tank game
但是当他看向不同的地方时,它看起来很奇怪:
tank looking right

tank looking down
我怎样才能修复它,使其适用于所有方向?

编辑:
在尝试了 Abhishek Aryan 的建议后(我的米像素是一个):

        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());

它看起来像这样:
tank problem1
tank problem2

最佳答案

b2body 是你的 box2d body 吗?如果是,则将米转换为像素以在屏幕上绘制。

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());

在这里,您使用 sprite Rotation 来旋转火焰动画。所以你需要根据 b2dbody 旋转来旋转你的 Sprite,这样你就可以在这里使用否则你需要以弧度获取 b2body 旋转并转换成度数并在这里使用。

我在这里测试了 TextureRegion 的偏移量

 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();
}
}

关于java - 来自枪的 Libgdx 爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42453665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com