动画中的过渡帧

时间:2013-12-10 04:49:54

标签: java animation 2d libgdx

好的,这将是一件非常容易的事,但出于某种原因,我只是没有抓住它。

我有一个常设动画,一个动画动画我想要的是在两个动画之间播放单帧过渡,我似乎无法完成它(用布尔人尝试过)

这是我的代码,也许有人可以提供帮助。

另外,我确实面向左/右分离动画,有没有办法可以翻转单个动画?

在noob编码器中提前致谢 - 继承我的代码并删除了一些不可靠的东西

public class Player extends Sprite implements InputProcessor {

private Vector2 velocity = new Vector2();
private float speed = 60 * 2, gravity = 60 * 2.2f, animationTime = 0;

private boolean canJump;
private boolean attacking;
private boolean starting = true;


private Animation still, left, right, attack, start;
private TiledMapTileLayer collisionLayer;



public Player(Animation still, Animation left, Animation right, Animation attack, Animation start, TiledMapTileLayer collisionLayer) {
    super(still.getKeyFrame(0));
    this.still = still;
    this.left = left;
    this.right = right;
    this.attack = attack;
    this.start = start;
    this.collisionLayer = collisionLayer;
    setSize(getWidth(), getHeight() /* * 1.5f*/);
    //setScale((float) 1.4);
}

@Override
public void draw(SpriteBatch spriteBatch) {
    update(Gdx.graphics.getDeltaTime());
    super.draw(spriteBatch);



    // update animation
    animationTime += delta;

         setRegion(

            velocity.x < 0 ? left.getKeyFrame(animationTime) :


            velocity.x > 0  ? right.getKeyFrame(animationTime) :

            attacking == true ? attack.getKeyFrame(animationTime) :
            still.getKeyFrame(animationTime));



}


@Override
public boolean keyDown(int keycode) {
    switch(keycode) {
    case Keys.W:
        if(canJump) {
            velocity.y = speed / 1.8f;
            canJump = false;
        }
        break;
    case Keys.A:
        velocity.x = -speed;
        animationTime = 0;
        break;
    case Keys.D:
        velocity.x = speed;
        animationTime = 0;





case Keys.I:
    attacking = true;
    animationTime = 0.15f;
}
    return true;
}

@Override
public boolean keyUp(int keycode) {
    switch(keycode) {
    case Keys.A:
    case Keys.D:
        velocity.x = 0;
        animationTime = 0;

    case Keys.I:
        animationTime = 0;
        attacking = false;
    }
    return true;
}

从我的屏幕上将其显示在地图上

@Override
public void show() {
    map = new TmxMapLoader().load("maps/map.tmx");



    camera = new OrthographicCamera();
    batch = new SpriteBatch();
    backimage = new Sprite(new Texture("images/backgrounds/background.png"));

    playerAtlas = new TextureAtlas("img/player/player.pack");
    Animation still, left, right, attack, start;
    still = new Animation(2 / 2f, playerAtlas.findRegions("still"));
    left = new Animation(1 / 6f, playerAtlas.findRegions("left"));
    right = new Animation(2 / 26f, playerAtlas.findRegions("right"));
    attack = new Animation(1 / 6f, playerAtlas.findRegions("attack"));
    start = new Animation(1 / 2f, playerAtlas.findRegions("start"));
    still.setPlayMode(Animation.LOOP);
    left.setPlayMode(Animation.LOOP);
    right.setPlayMode(Animation.LOOP);
    start.setPlayMode(Animation.NORMAL);
    attack.setPlayMode(Animation.NORMAL);

    player = new Player(still, left, right, attack, start, (TiledMapTileLayer) map.getLayers().get(0));

    MapLayer layer = map.getLayers().get("objects");
        for(MapObject object : layer.getObjects())
            if(object.getName().equals("playerstart"))
             player.setPosition(object.getProperties().get("x", Integer.class),  object.getProperties().get("y", Integer.class));


    Gdx.input.setInputProcessor(player);


}

1 个答案:

答案 0 :(得分:0)

我所做的是处理使用状态呈现的动画:

public static final int STATE_IDLE = 0;
public static final int STATE_WALKINGLEFT = 1;
public static final int STATE_WALKINGRIGHT = 2;
public static final int STATE_TRANSITIONLEFT = 3;
public static final int STATE_TRANSITIONRIGHT = 4;

public int state = STATE_IDLE;

然后使用输入将状态更改为转换:

case Keys.A:
    state = STATE_TRANSITIONLEFT;
    velocity.x = -speed;
    animationTime = 0;
    break;
case Keys.D:
    state = STATE_TRANSITIONRIGHT;
    velocity.x = speed;
    animationTime = 0;

在渲染方法中,等待一段时间并将过渡状态更改为步行状态:

if(animationTime>0.1F){ //your transition frame time
    if(state == STATE_TRANSITIONLEFT){
        state = STATE_WALKINGLEFT;
        animationTime = 0;
    }
    if(state == STATE_TRANSITIONRIGHT){
        state = STATE_WALKINGRIGHT;
        animationTime = 0;
    }
}

如果输入:

,则将状态返回空闲状态
case Keys.A:
case Keys.D:
    velocity.x = 0;
    state == STATE_IDLE;
    animationTime = 0;

最后,渲染当前动画:

switch(state){
    case STATE_IDLE:
        //draw idle animation
        break;
    case STATE_TRANSITIONLEFT:
        //draw transitionleft frame
        break;
    case STATE_WALKINGLEFT:
        //draw walkingleft animation
        break;
    //etc...
}

还有你的上一个问题。是的,你可以翻转你动画的区域:

TextureRegion#flip