2016-07-13 1 views
0

이전에 각 상태에 대해 별도의 .png 및 textureAtlas 파일을 사용하여 스프라이트의 여러 상태 (정지/보행/점프)에 애니메이션을 적용하고있었습니다. 그것은 잘 작동했다. 이제 저는이 별개의 시트를 하나의 PNG로 합치고 하나의 .pack 파일을 생성했습니다. 이제는 지역별로 이러한 상태에 액세스 할 수 있지만 더 이상 움직이지 않습니다. 전체 애니메이션이 아닌 각 상태의 첫 번째 스프라이트 만 그려집니다. 단일 스프라이트 시트를 사용하여 스프라이트 애니메이션하기

package com.mygdx.game; 

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.g2d.Animation; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureAtlas; 

public class MyGdxGame extends ApplicationAdapter implements InputProcessor { 
    private SpriteBatch batch; 
    private TextureAtlas textureAtlas; 
    private TextureAtlas textureAtlas2; 
    private Animation animation; 
    private Animation animation2; 
    private Animation currentAnimation; 
    private float elapsedTime = 0; 

    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     textureAtlas = new TextureAtlas(Gdx.files.internal("Wolverine.txt")); 
     animation = new Animation(1/7f, textureAtlas.findRegion("Standing")); 
     animation2 = new Animation(1/7f, textureAtlas.findRegion("Walking")); 
     currentAnimation = animation; 

     Gdx.input.setInputProcessor(this); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     textureAtlas.dispose(); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     batch.begin(); 
     //sprite.draw(batch); 
     elapsedTime += Gdx.graphics.getDeltaTime(); 
     batch.draw(currentAnimation.getKeyFrame(elapsedTime, true), 0, 0); 
     batch.end(); 
    } 

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

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
     if (keycode == Input.Keys.LEFT) { 
      currentAnimation = animation2; 
     } 
      return true; 

    } 

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

는 문제는 당신이 Animation 생성자 하나 개의 애니메이션 프레임을 제공하고 있다는 점이다 나는 spritesheet

Wolverine.png 
size: 256, 256 
format: RGBA8888 
filter: Linear,Linear 
repeat: none 
Standing 
    rotate: false 
    xy: 0, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 1 
Standing 
    rotate: false 
    xy: 64, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 2 
Standing 
    rotate: false 
    xy: 128, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 3 
Walking 
    rotate: false 
    xy: 192, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 1 
Walking 
    rotate: false 
    xy: 0, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 2 
Walking 
    rotate: false 
    xy: 64, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 3 
Walking 
    rotate: false 
    xy: 128, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 4 
Walking 
    rotate: false 
    xy: 192, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 5 
Walking 
    rotate: false 
    xy: 0, 128 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 6 
+0

당신의'.pack' 파일 내용을 첨부 해주세요 –

+0

좋아, 원래 게시물에 spritesheet에 사용하고있는 데이터 파일을 추가했습니다. –

답변

3

으로 사용하고 데이터 파일입니다. 정확하게하려면 다음 생성자를 사용하고 있습니다.

public Animation(float frameDuration, TextureRegion... keyFrames) 

프레임이 하나뿐입니다. 애니메이션에 애니메이션에 포함시키려는 모든 프레임을 전달해야합니다. 워킹 당신의 스프라이트를 걷는 또는 textureAtlas 모든 되고 있습니다 이름 만 상임 또는 내부

들은 예를 들어 이름을 지정해야 할 때 실제로

Standing0 
    Standing1 
    Standing2 
    ... 

Walking0 
    Walking1 
    Walking2 
    Walking3 
    ... 

그것들의 순서는 애니메이션에 있습니다. 텍스쳐 아틀라스에 포장하기 전에 애니메이션 파일의 이름을 바꾸거나 핫 픽스로 .pack 파일의 이름을 바꿀 수 있습니다.

그럼 당신은 너무 애니메이션에 통과 프레임처럼 모든 서/걷기를 얻을 수

public Array<TextureAtlas.AtlasRegion> findRegions(java.lang.String name) 

방법을 사용할 수 있습니다 :

textureAtlas.findRegions("Standing") 
+1

Sprites의 이름이 그대로있는 경우에도 데이터 시트가 작동한다는 것을 알았지 만 매우 부담 스럽습니다. 내가 잘못하고있는 것은'findRegions()'대신'findRegion()'을 호출하는 것입니다. –