Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java libGDX游戏-动画不';t翻转_Java_Android_Animation_Libgdx_Game Engine - Fatal编程技术网

Java libGDX游戏-动画不';t翻转

Java libGDX游戏-动画不';t翻转,java,android,animation,libgdx,game-engine,Java,Android,Animation,Libgdx,Game Engine,我有一个动画,如果按下左键,我想向左翻转,但它不会保持翻转状态。它只翻转一帧,然后再次翻转 这是我的游戏屏幕,我在这里绘制所有内容: public class GameScreen extends ScreenManager{ //For the view of the game and the rendering private SpriteBatch batch; private OrthographicCamera cam; //DEBUG pri

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

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

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



}
这是播放类,动画是:

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



}
公共类播放器{
公共静态主体;
公共静态BodyDef;
私人固定资产;
//质地
公共静态纹理;
公共静态精灵;
公共静态纹理区域[][]区域;
公共静态纹理区域[]空闲;
公共静态动画;
公共静态浮动状态时间;
//定形
私有多边形形状;
私人游戏机;
公共玩家(游戏世界){
纹理=新纹理(Gdx.files.internal(“player/char\u animation\u standing.png”);
region=TextureRegion.split(纹理,纹理.getWidth()/3,纹理.getHeight()/2);
空闲=新纹理区域[6];
int指数=0;
对于(int i=0;i<2;i++){
对于(int j=0;j<3;j++){
精灵=新精灵(区域[i][j]);
空闲[索引++]=精灵;
}
}
动画=新动画(1/8f,空闲);
stateTime=0f;
def=新车身def();
def.fixedRotation=true;
定义位置设置(gs.w/4,gs.h/4);
def.type=BodyType.DynamicBody;
body=world.getWorld().createBody(def);
形状=新的多边形形状();
形状设置框(32/2/PPM,64/2/PPM);
fd=新的FixtureDef();
fd.shape=形状;
fd.密度=30;
车身夹具(fd);
shape.dispose();
}
公共静态主体getBody(){
返回体;
}
公共静态BodyDef getDef(){
返回def;
}
公共静态纹理getTexture(){
返回纹理;
}
公共静态void键输入(浮点增量){
力=0;
if(Gdx.input.isKeyJustPressed(input.Keys.UP)){
applylinearpulse(0,300f,body.getWorldCenter().x,body.getWorldCenter().y,true);
//主体。应用力中心(0,1200f,真);
系统输出打印项次(“按下”);
}
如果(Gdx.input.isKeyPressed(input.Keys.LEFT)){
水平力-=1;
sprite.flip(!sprite.isFlipX(),sprite.isFlipY());
}
如果(Gdx.input.isKeyPressed(input.Keys.RIGHT)){
水平力+=1;
}
body.setLinearVelocity(horizonForce*20,body.getLinearVelocity().y);
}
}

提前感谢您,任何答案都将不胜感激:D

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

您仅通过
sprite
参考翻转动画的最后一帧,您需要翻转
动画的所有帧。您可以通过以下方式翻转:

if(keycode== Input.Keys.RIGHT) {

    for (TextureRegion textureRegion:anim.getKeyFrames())
        if(!textureRegion.isFlipX()) textureRegion.flip(true,false);
}
else if(keycode==Input.Keys.LEFT) {

    for (TextureRegion textureRegion:anim.getKeyFrames())
       if(textureRegion.isFlipX()) textureRegion.flip(true,false);
}

谢谢你的回答!如果它解决了你的问题,那么请接受它作为你问题的正确答案。你能告诉我如何让玩家只跳一次吗?