com.badlogic.gdx.graphics.g2d.Animation.getKeyFrame()方法的使用及代码示例

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

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

Animation.getKeyFrame介绍

[英]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 #setPlayMode(PlayMode) method.
[中]返回基于所谓状态时间的帧。这是对象在该动画实例表示的状态下所花费的秒数,例如,使用#setPlayMode(PlayMode)方法指定的模式跑步、跳跃等。

代码示例

代码示例来源: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

/** 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

private void renderKoala (float deltaTime) {
  // based on the koala state, get the animation frame
  TextureRegion frame = null;
  switch (koala.state) {
  case Standing:
    frame = stand.getKeyFrame(koala.stateTime);
    break;
  case Walking:
    frame = walk.getKeyFrame(koala.stateTime);
    break;
  case Jumping:
    frame = jump.getKeyFrame(koala.stateTime);
    break;
  }
  // draw the koala, depending on the current velocity
  // on the x-axis, draw the koala facing either right
  // or left
  Batch batch = renderer.getBatch();
  batch.begin();
  if (koala.facesRight) {
    batch.draw(frame, koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
  } else {
    batch.draw(frame, koala.position.x + Koala.WIDTH, koala.position.y, -Koala.WIDTH, Koala.HEIGHT);
  }
  batch.end();
}

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

@Override
public void render () {
  Gdx.gl.glClearColor(0.1f, 0f, 0.25f, 1f);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  batch.begin();
  for (int i = 0; i < cavemen.length; i++) {
    Caveman caveman = cavemen[i];
    TextureRegion frame = caveman.headsLeft ? leftWalk.getKeyFrame(caveman.stateTime, true) : rightWalk.getKeyFrame(
      caveman.stateTime, true);
    batch.draw(frame, caveman.pos.x, caveman.pos.y);
  }
  batch.end();
  for (int i = 0; i < cavemen.length; i++) {
    cavemen[i].update(Gdx.graphics.getDeltaTime());
  }
  fpsLog.log();
}

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

@Override
public void render () {
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  currentFrameTime += Gdx.graphics.getDeltaTime();
  spriteBatch.begin();
  TextureRegion frame = currentWalk.getKeyFrame(currentFrameTime, true);
  spriteBatch.draw(frame, position.x, position.y);
  spriteBatch.end();
}

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

@Override
public void render () {
  Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  int size = Math.min(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  int quarterSize = (int) (size/4f);
  batch.begin();
  batch.draw(textureRegions.get(pageToShow), 0, 0, size, size);
  ninePatch.draw(batch, 10, 10, quarterSize, quarterSize);
  officialPatch.draw(batch, (int)(size * 0.25f + 20), 10, quarterSize, quarterSize);
  batch.draw(animation.getKeyFrame(stateTime), 30 + (quarterSize * 2), 10, quarterSize, quarterSize);
  batch.end();
  shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
  shapeRenderer.setColor(Color.GREEN);
  shapeRenderer.rect(0, 0, size, size);
  shapeRenderer.end();
  stateTime += Gdx.graphics.getDeltaTime();
}

代码示例来源:origin: Catacomb-Snatch/Catacomb-Snatch

/**
 * returns the current keyframe for that animation
 *
 * @param index of the animation to pull from
 * @return a TextureRegion of the frame
 */
public TextureRegion getKeyFrame(int index) {
  return animations[index].getKeyFrame(stateTime);
}

代码示例来源:origin: stackoverflow.com

class InputHandler {

  public Animation animation;
  public TextureRegion region;

  public InputHandler() {

    Assets.load();  //loads textures from files into memory and creates animations from them
    animation = Assets.playStance;  //set initial animation to be used
    region = animation.getKeyFrame(0, true);    //make TextureRegion equal to first frame in animation

  }
}

代码示例来源:origin: Mknsri/Drunk-Toss

@Override
public void render(Screen g) {
  animTime += Gdx.graphics.getDeltaTime();
  if (collisionDetected) {
    pummiFrame = pummiTriggered.getKeyFrame(animTime);
    g.drawSprite(pummiFrame, x, y, 0);
  } else {
    pummiFrame = pummiIdle.getKeyFrame(animTime, true);
    g.drawSprite(pummiFrame, x, y, 0);
  }
}

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

public void render(SpriteBatch batch) {
  final float elapsedTime = Utils.secondsSince(startTime);
  final TextureRegion region = Assets.instance.exitPortalAssets.exitPortal.getKeyFrame(elapsedTime, true);
  Utils.drawTextureRegion(batch, region, position, Constants.EXIT_PORTAL_CENTER);
}

代码示例来源:origin: net.mostlyoriginal.artemis-odb/contrib-jam

protected void process(final int e) {
  final Animation animation = mAnimation.get(e);
  final AnimationAsset asset = mAnimationAsset.get(e);
  animation.age += world.delta;
  renderKeyframe(e, asset.asset.getKeyFrame(Math.abs(animation.age)));
}

代码示例来源:origin: DaanVanYperen/artemis-odb-contrib

protected void process(final int e) {
  final Animation animation = mAnimation.get(e);
  final AnimationAsset asset = mAnimationAsset.get(e);
  animation.age += world.delta;
  renderKeyframe(e, asset.asset.getKeyFrame(Math.abs(animation.age)));
}

代码示例来源:origin: yichen0831/Bomberman_libGdx

@Override
  public void act(float delta) {
    super.act(delta);
    stateTime += delta;
    setDrawable(new TextureRegionDrawable(anims.get(currentAnim).getKeyFrame(stateTime)));
  }
}

代码示例来源:origin: danialgoodwin/dev

@Override
public void draw(Batch batch, float parentAlpha) {
  super.draw(batch, parentAlpha);
  stateTime += Gdx.graphics.getDeltaTime();
  batch.draw(animation.getKeyFrame(stateTime, true), (screenRectangle.x - (screenRectangle.width * 0.1f)),
      screenRectangle.y, screenRectangle.width * 1.2f, screenRectangle.height * 1.1f);
}

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

public void render(SpriteBatch batch) {
  if (!isFinished() && !yetToStart()) {
    Utils.drawTextureRegion(
        batch,
        Assets.instance.explosionAssets.explosion.getKeyFrame(Utils.secondsSince(startTime) - offset),
        position.x - Constants.EXPLOSION_CENTER.x,
        position.y - Constants.EXPLOSION_CENTER.y
    );
  }
}

代码示例来源:origin: dsaltares/ashley-superjumper

@Override
  public void processEntity(Entity entity, float deltaTime) {
    TextureComponent tex = tm.get(entity);
    AnimationComponent anim = am.get(entity);
    StateComponent state = sm.get(entity);
    
    Animation animation = anim.animations.get(state.get());
    
    if (animation != null) {
      tex.region = animation.getKeyFrame(state.time); 
    }
    
    state.time += deltaTime;
  }
}

代码示例来源:origin: RoaringCatGames/libgdx-ashley-box2d-example

@Override
  protected void processEntity(Entity entity, float deltaTime) {

    AnimationComponent ani = am.get(entity);
    StateComponent state = sm.get(entity);

    if(ani.animations.containsKey(state.get())){
      TextureComponent tex = tm.get(entity);
      tex.region = ani.animations.get(state.get()).getKeyFrame(state.time, state.isLooping);
    }

    state.time += deltaTime;
  }
}

代码示例来源: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: BrentAureli/SuperMario

public void update(float dt){
  stateTime += dt;
  setRegion(fireAnimation.getKeyFrame(stateTime, true));
  setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
  if((stateTime > 3 || setToDestroy) && !destroyed) {
    world.destroyBody(b2body);
    destroyed = true;
  }
  if(b2body.getLinearVelocity().y > 2f)
    b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f);
  if((fireRight && b2body.getLinearVelocity().x < 0) || (!fireRight && b2body.getLinearVelocity().x > 0))
    setToDestroy();
}

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

public void draw(Batch batch, float a) {
    batch.setColor(getColor().r, getColor().g, getColor().b, getColor().a
        * a);
    batch.draw(animation.getKeyFrame(stateTime), getX(), getY(),
        getOriginX(), getOriginY(), getWidth(), getHeight(),
        getScaleX(), getScaleY(), getRotation());
  }
}

相关文章