현재 앱을 실행하자마자 실행되는 애니메이션이 있습니다. 이제 사용자 입력에 따라 재생되는 다른 애니메이션을 선택하고 싶습니다. 예 : 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;
}
}
TextureAtlases는 참조를 잃거나 메모리 누수가 발생하기 전에 처리해야합니다. 'textureAtlas = new TextureAtlas ... '를 호출하면 인스턴스화 한 첫 번째 TextureAtlas에 대한 참조가 손실됩니다. – Tenfour04
그럼 어떻게 방지 할 수 있습니까? 내가 처리 할 수 있도록 첫 번째 TextureAtlas를 보유 할 세 번째 textureAtlas를 사용해야합니까? 아니면 이것을하는 더 좋은 방법이 있습니까? –
참조를 다른 것으로 변경하기 전에 처리를 요청하십시오. 일반적으로 스테이지를 전환 할 때만이 작업을 수행합니다. 게임의 규모가 크지 않다면 아마 아틀라스 하나만 있으면됩니다. AssetManager에서 읽으십시오. – Tenfour04