sprite.java

来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· Java 代码 · 共 92 行

JAVA
92
字号
import javax.microedition.lcdui.Graphics;

public class Sprite
{
  private ImageSet imageSet;
  private int currentState;
  private int currentFrame;
  private int totalCycles;
  private long currentStateBegin;
  private long lastFrameChange;

  public Sprite(ImageSet imageSet, int startState, int startFrame)
  {
    this.imageSet = imageSet;
    setCurrentState(startState, true);
    setCurrentFrame(startFrame);
  }

  public void setCurrentFrame(int frame)
  {
    currentFrame = frame;
  }

  public void setCurrentState(int state, boolean force)
  {
    if (currentState != state || force)
    {
      currentState = state;
      currentFrame = 0;
      totalCycles = 0;
      currentStateBegin = System.currentTimeMillis();
    }
  }

  public void reset()
  {
    currentFrame = 0;
    totalCycles = 0;
    currentStateBegin = 0;
    lastFrameChange = 0;
  }

  public long getWhenStateBegin()
  {
    return currentStateBegin;
  }

  public long getTimeInCurrentState()
  {
    return (System.currentTimeMillis() - currentStateBegin);
  }

  public int getCurrentState()
  {
    return currentState;
  }

  public int getCurrentFrame()
  {
    return currentFrame;
  }

  public void draw(Graphics gra, int targetX, int targetY, int anchor)
  {
    imageSet.draw(gra, currentState, currentFrame, targetX, targetY, anchor);
  }

  public void cycle()
  {
    if (imageSet.getTotalFrames(currentState) > 1 &&
        imageSet.getAnimTime(currentState) > 0)
    {
      long deltaTime = System.currentTimeMillis() - lastFrameChange;
      if (deltaTime > imageSet.getAnimTimePerFrame(currentState))
      {
        currentFrame++;
        lastFrameChange = System.currentTimeMillis();
        if (currentFrame >= imageSet.getTotalFrames(currentState))
        {
          currentFrame = 0;
          totalCycles++;
        }
      }
    }
  }

  public int getTotalCycles()
  {
    return totalCycles;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?