2016-07-07 1 views
0

현재 앱을 실행하자마자 실행되는 애니메이션이 있습니다. 이제 사용자 입력에 따라 재생되는 다른 애니메이션을 선택하고 싶습니다. 예 : left 키를 누르면 왼쪽으로 걷는 애니메이션이 현재 스프라이트의 애니메이션을 대체합니다. keyDown 함수에 새로운 textureAtlas을 추가하려고 시도했지만 작동하지 않습니다.입력에 대한 응답으로 애니메이션이 변경됩니다.

다른 말로하면 입력이 없을 때 응용 프로그램이 스프라이트의 유휴 애니메이션을 표시하기를 원합니다. 그런 다음 버튼을 누르면 (예를 들어 왼쪽) 걷는 애니메이션으로 바뀌어야합니다. 당신처럼 -

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 Animation animation; 
    private float elapsedTime = 0; 

    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheet.atlas")); 
     animation = new Animation(1/7f, textureAtlas.getRegions()); 
    } 

    @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(animation.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) { 
      textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheet2.atlas")); 
     } 
      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; 
    } 
} 
+0

TextureAtlases는 참조를 잃거나 메모리 누수가 발생하기 전에 처리해야합니다. 'textureAtlas = new TextureAtlas ... '를 호출하면 인스턴스화 한 첫 번째 TextureAtlas에 대한 참조가 손실됩니다. – Tenfour04

+0

그럼 어떻게 방지 할 수 있습니까? 내가 처리 할 수 ​​있도록 첫 번째 TextureAtlas를 보유 할 세 번째 textureAtlas를 사용해야합니까? 아니면 이것을하는 더 좋은 방법이 있습니까? –

+0

참조를 다른 것으로 변경하기 전에 처리를 요청하십시오. 일반적으로 스테이지를 전환 할 때만이 작업을 수행합니다. 게임의 규모가 크지 않다면 아마 아틀라스 하나만 있으면됩니다. AssetManager에서 읽으십시오. – Tenfour04

답변

1

당신이 얼마나 많은 다른 애니메이션에 따라, 당신은 단지 모든 textureAtlases를로드 할 수 있으며, 다양한 애니메이션을 만들 수 (어쩌면 그것은 단지 유휴, walk_right는, walk_left은, 다른 사람의 어쩌면 몇입니다 ...)

//simple example ... 
animationIdle = new Animation(1/7f, textureAtlas1.getRegions()); 
animationLeft = new Animation(1/7f, textureAtlas2.getRegions()); 
//etc 

그런 다음 (렌더링 방법이 아닌) 어딘가에 코드를 가지고 "현재 애니메이션"이 무엇인지 결정하십시오. 예를 들어 코드를 사용 :

batch.draw(currentAnimation.getKeyFrame(elapsedTime, true), 0, 0); 
//increment elapsedTime after drawing animation at least once?? 
elapsedTime += Gdx.graphics.getDeltaTime(); 

:

@Override 
public boolean keyDown(int keycode) { 
    if (keycode == Input.Keys.LEFT) { 
    // Maybe check to make sure the currentAnimation is not already the 
    // left animation, if it isn't, swap it: 
     currentAnimation = animationLeft; 
     //set elapsedTime to 0 to "Restart the new animation" at its beginning 
     elapsedTime = 0; 
    } 

그런 다음 당신이 방법은 단지 현재 애니메이션을 그립니다, 필요에 따라 (당신은 당신이 원하는 애니메이션을 변경하는 경우) elapsedTime 재설정 된) (렌더링 위의 코드는 구문이 없거나 최상의 구조로 작성된 것은 아니지만 작동 원리를 알려줍니다.

관련 문제