【问题标题】:How to play an animation frames for once each time?-LibGdx如何每次播放一次动画帧?-LibGdx
【发布时间】:2018-04-09 01:56:59
【问题描述】:

我在我的游戏中使用 6 帧动画来进行事件。它有固定的播放顺序。

Array<TextureAtlas.AtlasRegion> anim=new Array<TextureAtlas.AtlasRegion>();
anim =  game.playAtlas.findRegions(RegionNames.FEATHER_ANIMATION);
TextureRegion[] ftr = {anim.get(0),anim.get(1),anim.get(2),anim.get(3),anim.get(4),anim.get(5)};

featherAnimation = new Animation(0.1f,ftr);
featherAnimation.setPlayMode(PlayMode.NORMAL);

我只想运行这个动画一次(这意味着它应该播放第 0 帧到 最后一帧,然后应该停止),当特定的布尔值为真时。

播放完最后一帧后,它应该停止并使布尔值也为假。

我就是这样做的

if (featherBool && egg.isEggAtNest()) {
  featherTexture = featherAnimation.getKeyFrame(animationTime, true);
  batch.draw(featherTexture, egg.getX() - featherTexture.getRegionWidth() / 2, fx.getY());
  featherBool = false;
}

但这并没有达到我想要的效果。

帧的顺序不是我指定的。 另外,播放 0 到 5th 帧后如何停止动画?

【问题讨论】:

    标签: java animation libgdx


    【解决方案1】:

    您是否将您的animationTime 值用于其他动画? 如果是,则应在播放featherAnimation 之前将其设置为0。

    if (somethingToRunfeatherAnimation) {
       animationTime = 0;
       featherBool = true;
    }
    

    此外,这个 sn-p 将只运行一次,因为您一次设置了 featherBool = false

    if (featherBool && egg.isEggAtNest()) {
       featherTexture = featherAnimation.getKeyFrame(animationTime, true);
       batch.draw(featherTexture, egg.getX() - featherTexture.getRegionWidth() / 2, fx.getY());
       featherBool = false;
    }
    

    像这样使用它:

    if (featherBool && egg.isEggAtNest() && !featherAnimation.isAnimationFinished(animationTime) {
       featherTexture = featherAnimation.getKeyFrame(animationTime); // deleted looping
       batch.draw(featherTexture, egg.getX() - featherTexture.getRegionWidth() / 2, fx.getY());
    }
    

    【讨论】:

      【解决方案2】:

      这样使用:

      if (featherBool) {  
          animationTime+=.01f;
          featherTexture = featherAnimation.getKeyFrame(animationTime);  // <--looping not required so remove second argument
          batch.draw(featherTexture, egg.getX() - featherTexture.getRegionWidth() / 2, fx.getY());
          if(featherAnimation.isAnimationFinished(animationTime))
              featherBool = false;
      }
      

      调用showAnimation()方法

      private void showAnimation(){
      
          featherBool=true;
          animationTime=0;
      }
      

      【讨论】:

        【解决方案3】:

        渲染代码只被调用一次

        if (featherBool && egg.isEggAtNest()) {
           featherTexture = featherAnimation.getKeyFrame(animationTime, true);
           batch.draw(featherTexture, egg.getX() - featherTexture.getRegionWidth() / 2, fx.getY());
           featherBool = false; ///   The render code is called only once
        }
        

        你也必须提高时间。

        stateTime += Gdx.graphics.getDeltaTime();
        

        在你设置错误的featherBool值之前,你必须放置动画端点

        if( yourAnimation.isAnimationFinished( stateTime ) )
                featherBool = false;
        

        【讨论】:

          猜你喜欢
          • 2021-12-15
          • 2013-05-22
          • 1970-01-01
          • 2013-09-25
          • 1970-01-01
          • 2013-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多