2013-12-10 98 views
1

好吧,这将是一件愚蠢的事情,但由于某种原因,我只是没有抓住它。动画中的过渡帧

我有一个常设的动画,和移动的动画我想是玩其间的两个动画的单个帧过渡,我似乎无法完成它

这里是我的代码也许(与布尔试过)有人可以帮忙。

此外,我做单独的动画面对左/右,有没有一种方法,我可以只翻转单个动画?

感谢从一个小白编码器提前 - 我的继承人有一些irrelivent填充码切出

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); 


} 

回答

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; 

然后将状态更改为转换w第i个输入:

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

+0

顺便说一句,其余工作精美 – user2873364