gpt4 book ai didi

Java libGDX 游戏 - 动画不翻转

转载 作者:行者123 更新时间:2023-12-02 12:55:11 25 4
gpt4 key购买 nike

我有一个动画,如果按下左键,我想向左翻转,但它不会保持翻转状态。它只翻转 1 帧,然后再转回来。

这是我的游戏屏幕,我在其中绘制所有内容:

public class GameScreen extends ScreenManager{
//For the view of the game and the rendering
private SpriteBatch batch;
private OrthographicCamera cam;

//DEBUG
private Box2DDebugRenderer b2dr;

//World, Player and so on
private GameWorld world;
private Player player;
private Ground ground;

//player animations
private TextureRegion currFrame;

public static float w, h;

public GameScreen(Game game) {
super(game);
//vars
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();

//view and rendering
batch = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, w/2, h/2);

//debug
b2dr = new Box2DDebugRenderer();

//world, bodies ...
world = new GameWorld();
player = new Player(world);
ground = new Ground(world);

}

@Override
public void pause() {


}

@Override
public void show() {


}

@Override
public void render(float delta) {
//clearing the screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//updating
update(Gdx.graphics.getDeltaTime());
player.stateTime += Gdx.graphics.getDeltaTime();

//render
batch.setProjectionMatrix(cam.combined);

currFrame = Player.anim.getKeyFrame(Player.stateTime, true);

batch.begin();
batch.draw(currFrame, Player.body.getPosition().x * PPM - 64, Player.getBody().getPosition().y * PPM- 72);
batch.end();

//debug
b2dr.render(GameWorld.getWorld(), cam.combined.scl(PPM));


}

@Override
public void resize(int width, int height) {


}

@Override
public void hide() {


}


@Override
public void dispose() {


}

@Override
public void onKlick(float delta) {


}

public void update(float delta){
world.update(delta);
updateCam(delta);
Player.keyInput(delta);
System.out.println("X-POS" + Player.getBody().getPosition().x);
System.out.println("Y-POS" + Player.getBody().getPosition().y);
}

public void updateCam(float delta){
Vector3 pos = cam.position;
pos.x = Player.getBody().getPosition().x * PPM;
pos.y = Player.getBody().getPosition().y * PPM;

cam.position.set(pos);

cam.update();
}



}

这是动画所在的 Player 类:

public class Player {
public static Body body;
public static BodyDef def;
private FixtureDef fd;

//textures
public static Texture texture;
public static Sprite sprite;
public static TextureRegion[][] region;
public static TextureRegion[] idle;
public static Animation<TextureRegion> anim;
public static float stateTime;

//set form
private PolygonShape shape;

private GameScreen gs;

public Player(GameWorld world){
texture = new Texture(Gdx.files.internal("player/char_animation_standing.png"));
region = TextureRegion.split(texture, texture.getWidth() / 3, texture.getHeight() / 2);
idle = new TextureRegion[6];
int index = 0;

for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
sprite = new Sprite(region[i][j]);
idle[index++] = sprite;
}
}

anim = new Animation<TextureRegion>(1 / 8f, idle);

stateTime = 0f;

def = new BodyDef();
def.fixedRotation = true;
def.position.set(gs.w / 4, gs.h / 4);
def.type = BodyType.DynamicBody;

body = world.getWorld().createBody(def);

shape = new PolygonShape();
shape.setAsBox(32 / 2 / PPM, 64/ 2 / PPM);

fd = new FixtureDef();
fd.shape = shape;
fd.density = 30;

body.createFixture(fd);
shape.dispose();
}

public static Body getBody() {
return body;
}

public static BodyDef getDef() {
return def;
}



public static Texture getTexture() {
return texture;
}

public static void keyInput(float delta){
int horizonForce = 0;
if(Gdx.input.isKeyJustPressed(Input.Keys.UP)){
body.applyLinearImpulse(0, 300f, body.getWorldCenter().x, body.getWorldCenter().y, true);
//body.applyForceToCenter(0, 1200f, true);
System.out.println("PRESSED");
}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
horizonForce -= 1;
sprite.flip(!sprite.isFlipX(), sprite.isFlipY());
}

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
horizonForce += 1;
}

body.setLinearVelocity(horizonForce * 20, body.getLinearVelocity().y);
}



}

提前感谢您,如有任何答复,我们将不胜感激:D

最佳答案

按下左键时,您的 Sprite 变量仅包含一帧。因此,它会翻转动画帧的当前 Sprite 。要解决这个问题,您必须在按左键时翻转所有动画帧。

关于Java libGDX 游戏 - 动画不翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450922/

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