com.badlogic.gdx.graphics.g2d.Animation类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中com.badlogic.gdx.graphics.g2d.Animation类的一些代码示例,展示了Animation类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Animation类的具体详情如下:
包路径:com.badlogic.gdx.graphics.g2d.Animation
类名称:Animation

Animation介绍

[英]An Animation stores a list of objects representing an animated sequence, e.g. for running or jumping. Each object in the Animation is called a key frame, and multiple key frames make up the animation.

The animation's type is the class representing a frame of animation. For example, a typical 2D animation could be made up of com.badlogic.gdx.graphics.g2d.TextureRegion and would be specified as:

Animation<TextureRegion> myAnimation = new Animation<TextureRegion>(...);
[中]动画存储表示动画序列的对象列表,例如用于跑步或跳跃的对象。动画中的每个对象称为关键帧,多个关键帧构成动画。
动画的类型是表示动画帧的类。例如,典型的2D动画可以由com组成。糟糕的逻辑。gdx。图样g2d。纹理区域,并将指定为:
Animation<TextureRegion> myAnimation = new Animation<TextureRegion>(...);

代码示例

代码示例来源:origin: libgdx/libgdx

/** Returns a frame based on the so called state time. This is the amount of seconds an object has spent in the
 * state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is
 * looping or not.
 * 
 * @param stateTime the time spent in the state represented by this animation.
 * @param looping whether the animation is looping or not.
 * @return the frame of animation for the given state time. */
public T getKeyFrame (float stateTime, boolean looping) {
  // we set the play mode by overriding the previous mode based on looping
  // parameter value
  PlayMode oldPlayMode = playMode;
  if (looping && (playMode == PlayMode.NORMAL || playMode == PlayMode.REVERSED)) {
    if (playMode == PlayMode.NORMAL)
      playMode = PlayMode.LOOP;
    else
      playMode = PlayMode.LOOP_REVERSED;
  } else if (!looping && !(playMode == PlayMode.NORMAL || playMode == PlayMode.REVERSED)) {
    if (playMode == PlayMode.LOOP_REVERSED)
      playMode = PlayMode.REVERSED;
    else
      playMode = PlayMode.LOOP;
  }
  T frame = getKeyFrame(stateTime);
  playMode = oldPlayMode;
  return frame;
}

代码示例来源:origin: libgdx/libgdx

@Override
public void create () {
  // load the koala frames, split them, and assign them to Animations
  koalaTexture = new Texture("data/maps/tiled/super-koalio/koalio.png");
  TextureRegion[] regions = TextureRegion.split(koalaTexture, 18, 26)[0];
  stand = new Animation(0, regions[0]);
  jump = new Animation(0, regions[1]);
  walk = new Animation(0.15f, regions[2], regions[3], regions[4]);
  walk.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
  // figure out the width and height of the koala for collision
  // detection and rendering by converting a koala frames pixel
  // size into world units (1 unit == 16 pixels)
  Koala.WIDTH = 1 / 16f * regions[0].getRegionWidth();
  Koala.HEIGHT = 1 / 16f * regions[0].getRegionHeight();
  // load the map, set the unit scale to 1/16 (1 unit == 16 pixels)
  map = new TmxMapLoader().load("data/maps/tiled/super-koalio/level1.tmx");
  renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);
  // create an orthographic camera, shows us 30x20 units of the world
  camera = new OrthographicCamera();
  camera.setToOrtho(false, 30, 20);
  camera.update();
  // create the Koala we want to move around the world
  koala = new Koala();
  koala.position.set(20, 20);
  debugRenderer = new ShapeRenderer();
}

代码示例来源:origin: libgdx/libgdx

/** Returns a frame based on the so called state time. This is the amount of seconds an object has spent in the
 * state this Animation instance represents, e.g. running, jumping and so on using the mode specified by
 * {@link #setPlayMode(PlayMode)} method.
 * 
 * @param stateTime
 * @return the frame of animation for the given state time. */
public T getKeyFrame (float stateTime) {
  int frameNumber = getKeyFrameIndex(stateTime);
  return keyFrames[frameNumber];
}

代码示例来源:origin: libgdx/libgdx

@Override
public void create () {
  texture = new Texture(Gdx.files.internal("data/walkanim.png"));
  TextureRegion[] leftWalkFrames = TextureRegion.split(texture, 64, 64)[0];
  Array<TextureRegion> rightWalkFrames = new Array(TextureRegion.class);
  for (int i = 0; i < leftWalkFrames.length; i++) {
    TextureRegion frame = new TextureRegion(leftWalkFrames[i]);
    frame.flip(true, false);
    rightWalkFrames.add(frame);
  }
  leftWalk = new Animation<TextureRegion>(0.25f, leftWalkFrames);
  rightWalk = new Animation<TextureRegion>(0.25f, rightWalkFrames);
  
  TextureRegion[] rightRegions = rightWalk.getKeyFrames(); // testing backing array type
  TextureRegion firstRightRegion = rightRegions[0];
  Gdx.app.log("AnimationTest", "First right walk region is " + firstRightRegion.getRegionWidth() + "x" + firstRightRegion.getRegionHeight());
  cavemen = new Caveman[100];
  for (int i = 0; i < 100; i++) {
    cavemen[i] = new Caveman((float)Math.random() * Gdx.graphics.getWidth(),
      (float)Math.random() * Gdx.graphics.getHeight(), Math.random() > 0.5 ? true : false);
  }
  batch = new SpriteBatch();
  fpsLog = new FPSLogger();
}

代码示例来源:origin: libgdx/libgdx

@Override
public void create () {
  Gdx.input.setInputProcessor(this);
  texture = new Texture(Gdx.files.internal("data/animation.png"));
  TextureRegion[][] regions = TextureRegion.split(texture, 32, 48);
  TextureRegion[] downWalkReg = regions[0];
  TextureRegion[] leftWalkReg = regions[1];
  TextureRegion[] rightWalkReg = regions[2];
  TextureRegion[] upWalkReg = regions[3];
  downWalk = new Animation<TextureRegion>(ANIMATION_SPEED, downWalkReg);
  leftWalk = new Animation<TextureRegion>(ANIMATION_SPEED, leftWalkReg);
  rightWalk = new Animation<TextureRegion>(ANIMATION_SPEED, rightWalkReg);
  upWalk = new Animation<TextureRegion>(ANIMATION_SPEED, upWalkReg);
  currentWalk = leftWalk;
  currentFrameTime = 0.0f;
  spriteBatch = new SpriteBatch();
  position = new Vector2();
}

代码示例来源:origin: libgdx/libgdx

/** Constructor, storing the frame duration and key frames.
 * 
 * @param frameDuration the time between frames in seconds.
 * @param keyFrames the objects representing the frames. If this Array is type-aware, {@link #getKeyFrames()} can
 * return the correct type of array. Otherwise, it returns an Object[].*/
public Animation (float frameDuration, Array<? extends T> keyFrames, PlayMode playMode) {
  this(frameDuration, keyFrames);
  setPlayMode(playMode);
}

代码示例来源:origin: xietansheng/FlappyBirdForGDX

@Override
public void act(float delta) {
  super.act(delta);
  if (animation != null) {
    TextureRegion region = null;
    if (isPlayAnimation) {
      // 如果需要播放动画, 则累加时间步, 并按累加值获取需要显示的关键帧
      stateTime += delta;
      region = animation.getKeyFrame(stateTime);
    } else {
      // 不需要播放动画, 则获取 fixedShowKeyFrameIndex 指定的关键帧 
      TextureRegion[] keyFrames = animation.getKeyFrames();
      if (fixedShowKeyFrameIndex >= 0 && fixedShowKeyFrameIndex < keyFrames.length) {
        region = keyFrames[fixedShowKeyFrameIndex];
      }
    }
    // 设置当前需要显示的关键帧
    setRegion(region);
  }
}

代码示例来源:origin: BrentAureli/SuperMario

break;
case GROWING:
  region = growMario.getKeyFrame(stateTimer);
  if(growMario.isAnimationFinished(stateTimer)) {
    runGrowAnimation = false;
  break;
case RUNNING:
  region = marioIsBig ? bigMarioRun.getKeyFrame(stateTimer, true) : marioRun.getKeyFrame(stateTimer, true);
  break;
case FALLING:

代码示例来源:origin: BrentAureli/SuperMario

public FireBall(PlayScreen screen, float x, float y, boolean fireRight){
  this.fireRight = fireRight;
  this.screen = screen;
  this.world = screen.getWorld();
  frames = new Array<TextureRegion>();
  for(int i = 0; i < 4; i++){
    frames.add(new TextureRegion(screen.getAtlas().findRegion("fireball"), i * 8, 0, 8, 8));
  }
  fireAnimation = new Animation(0.2f, frames);
  setRegion(fireAnimation.getKeyFrame(0));
  setBounds(x, y, 6 / MarioBros.PPM, 6 / MarioBros.PPM);
  defineFireBall();
}

代码示例来源:origin: libgdx/libgdx

/** Constructor, storing the frame duration and key frames.
 * 
 * @param frameDuration the time between frames in seconds.
 * @param keyFrames the objects representing the frames. */
public Animation (float frameDuration, T... keyFrames) {
  this.frameDuration = frameDuration;
  setKeyFrames(keyFrames);
}

代码示例来源:origin: xietansheng/FlappyBirdForGDX

public void setAnimation(Animation animation) {
  this.animation = animation;
  // 默认先显示第 0 帧
  if (this.animation != null) {
    TextureRegion[] keyFrames = this.animation.getKeyFrames();
    if (keyFrames.length > 0) {
      setRegion(keyFrames[0]);
    }
  }
}

代码示例来源:origin: udacity/ud406

public boolean isFinished() {
    float elapsedTime = Utils.secondsSince(startTime) - offset;
    return Assets.instance.explosionAssets.explosion.isAnimationFinished(elapsedTime);
  }
}

代码示例来源:origin: libgdx/libgdx

jumpAtlas = new TextureAtlas(Gdx.files.internal("data/jump.txt"));
jumpAnimation = new Animation<TextureRegion>(0.25f, jumpAtlas.findRegions("ALIEN_JUMP_"));

代码示例来源:origin: libgdx/libgdx

/** Constructor, storing the frame duration and key frames.
 * 
 * @param frameDuration the time between frames in seconds.
 * @param keyFrames the objects representing the frames. If this Array is type-aware, {@link #getKeyFrames()} can
 * return the correct type of array. Otherwise, it returns an Object[].*/
public Animation (float frameDuration, Array<? extends T> keyFrames, PlayMode playMode) {
  this(frameDuration, keyFrames);
  setPlayMode(playMode);
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private void updateCaveman() {
  if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) {
    if(goingRight) {
      for(TextureRegion t : cavemanWalk.getKeyFrames())
        t.flip(true, false);
      goingRight = false;
    }
    cavemanX -= Gdx.graphics.getDeltaTime() * cavemanSpeed;
    currentFrame = cavemanWalk.getKeyFrame(animationTime, true);
  }
  if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) {
    if(!goingRight) {
      goingRight = true;
      for(TextureRegion t : cavemanWalk.getKeyFrames())
        t.flip(true, false);
    }
    cavemanX += Gdx.graphics.getDeltaTime() * cavemanSpeed;
    currentFrame = cavemanWalk.getKeyFrame(animationTime, true);
  }
}

代码示例来源:origin: moribitotech/MTX

TextureRegion keyFrame = animation.getKeyFrame(stateTime,
    isAnimationLooping);
    getRotation());
if (animation.isAnimationFinished(stateTime)) {
  if (killAllAnimations) {
    isAnimationActive = false;
if (animationMomentary.isAnimationFinished(stateTime)) {
  if (!killAllAnimations) {
    isAnimationActive = true;
  TextureRegion keyFrame = animationMomentary.getKeyFrame(
      stateTime, false);

代码示例来源:origin: libgdx/libgdx

/** Constructor, storing the frame duration and key frames.
 * 
 * @param frameDuration the time between frames in seconds.
 * @param keyFrames the objects representing the frames. */
public Animation (float frameDuration, T... keyFrames) {
  this.frameDuration = frameDuration;
  setKeyFrames(keyFrames);
}

代码示例来源:origin: Var3D/var3dframe

/**
 * 动画播放frequency次后执行一个事件
 *
 * @param frequency
 */
public void setRunnableAction(int frequency, RunnableAction end) {
  float time = frameTime * animation.getKeyFrames().length * frequency;
  addAction(Actions.sequence(Actions.delay(time), end));
}

代码示例来源:origin: udacity/ud406

public boolean isAnimationFinished() {
  return animation.isAnimationFinished(elapsedTime());
}

代码示例来源:origin: libgdx/libgdx

/** Returns a frame based on the so called state time. This is the amount of seconds an object has spent in the
 * state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is
 * looping or not.
 * 
 * @param stateTime the time spent in the state represented by this animation.
 * @param looping whether the animation is looping or not.
 * @return the frame of animation for the given state time. */
public T getKeyFrame (float stateTime, boolean looping) {
  // we set the play mode by overriding the previous mode based on looping
  // parameter value
  PlayMode oldPlayMode = playMode;
  if (looping && (playMode == PlayMode.NORMAL || playMode == PlayMode.REVERSED)) {
    if (playMode == PlayMode.NORMAL)
      playMode = PlayMode.LOOP;
    else
      playMode = PlayMode.LOOP_REVERSED;
  } else if (!looping && !(playMode == PlayMode.NORMAL || playMode == PlayMode.REVERSED)) {
    if (playMode == PlayMode.LOOP_REVERSED)
      playMode = PlayMode.REVERSED;
    else
      playMode = PlayMode.LOOP;
  }
  T frame = getKeyFrame(stateTime);
  playMode = oldPlayMode;
  return frame;
}

相关文章