2016-07-07 46 views
0

我目前有一个动画,只要我启动应用程序就会运行。现在我想选择另一个播放以响应用户输入的动画。例如,如果我按左键,左边的动画会替换当前的精灵动画。我试过在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

在将引用改为别的之前调用它。通常,只有在切换阶段时才会这样做。如果你的游戏不是很大,你可能应该只有一个Atlas。在AssetManager上阅读。 – Tenfour04

回答

1

取决于你有多少个不同的动画(也许这只是闲置,walk_right,walk_left,也许其他几个...),你可以只加载所有textureAtlases和创造各种动画 - 喜欢你在create()方法没有

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

然后有一些代码的地方(而不是在渲染方法),确定了“当前动画”应该是什么。使用您的代码为例:

@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重置(如果你改变了你想要的动画):

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

上面的代码很粗糙,不一定是没有语法的或者结构最好的,但是可以让你了解什么可能工作。