2017-06-09 20 views
0

キーLEFTを押すと左に反転したいアニメーションがありますが、反転していません。それは1フレームのようにフリップし、その後再び戻ってくる。Java libGDX Game - アニメーションが反転しない

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



} 

は、事前にあなたに感謝し、すべての答えが理解される:D

ここ

は私がすべてを描く私のGameScreenです

答えて

1

あなたのスプライト変数には、左キーを押したときに1つのフレームのみが含まれています。つまり、アニメーションフレームの現在のスプライトを反転させます。 問題を解決するには、左のキーを押しながらすべてのアニメーションフレームを反転する必要があります。

+0

Ouuhh、あなたのために非常に答えてくれてありがとう! –

+0

問題が解決する場合は、質問の正解としてください。 – arv

+0

Playerに一度だけジャンプする方法を教えてもらえますか? –

0

アニメーションの最後のフレームを反転するのはspriteです。Animationanimのすべてのフレームを反転する必要があります。あなたはこの方法で反転することができます:

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

あなたの答えをありがとう! –

関連する問題