⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsspritebase.java~112~

📁 J2ME写的是男人就跳100层游戏
💻 JAVA~112~
字号:
package genius.javagame;

import javax.microedition.lcdui.*;

/**
* Class use Method:
* 1. public mySprite extends gsSpriteBase
* 2. private mySprite myspr1;
* 3. myspr1 = new mySprite("/res/images/sp1.png", 4, 3);
* 4. myspr1.setFrameDelay(50);
* 5. myspr1.DoAI();
* 6. myspr1.setXY(100, 200);
* . In the paint function: myspr1.Render(m_nSpriteX, m_nSpriteY, g);
* */
abstract public class gsSpriteBase {
  protected Image m_imgSprite = null;

  protected int m_nImageWidth = 0;
  protected int m_nImageHeight = 0;
  protected int m_nSpriteWidth = 0;
  protected int m_nSpriteHeight = 0;

  protected int m_nFramesCount = 0;
  protected int m_nRotationsCount = 0;
  private int m_nFrameDelay = 30;

  private long m_lLastTick = 0;
  protected int m_nCurFrame = 0;
  protected int m_nCurRotation = 0;

  protected int m_nX = 0;
  protected int m_nY = 0;

  protected boolean m_bPaused = false;

  public gsSpriteBase() {
  }

  public gsSpriteBase(String strFileName, int nFramesCount, int nRotationsCount) {
    m_nFramesCount = nFramesCount;
    m_nRotationsCount = nRotationsCount;

    if (true == LoadImage(strFileName)) {
    } else {
      m_nFramesCount = 0;
      m_nRotationsCount = 0;
    }
  }

  public boolean LoadImage(String strFileName, int nFramesCount, int nRotationsCount) {
    m_imgSprite = null;

    m_nFramesCount = nFramesCount;
    m_nRotationsCount = nRotationsCount;

    if (true == LoadImage(strFileName)) {
      return true;
    } else {
      m_nFramesCount = 0;
      m_nRotationsCount = 0;
    }

    return false;
  }

  public Image LoadImage(int nFramesCount, int nRotationsCount, String strImageName) {
    Image img = null;
    try {
      img = Image.createImage(strImageName);

      m_nImageWidth = img.getWidth();
      m_nImageHeight = img.getHeight();

      m_nFramesCount = nFramesCount;
      m_nRotationsCount = nRotationsCount;

      m_nSpriteWidth = m_nImageWidth / m_nFramesCount;
      m_nSpriteHeight = m_nImageHeight / m_nRotationsCount;
    }
    catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return img;
  }

  public boolean LoadImage(String strImageName) {
    try {
      m_imgSprite = null;
      m_imgSprite = Image.createImage(strImageName);

      m_nImageWidth = m_imgSprite.getWidth();
      m_nImageHeight = m_imgSprite.getHeight();

      m_nSpriteWidth = m_nImageWidth / m_nFramesCount;
      m_nSpriteHeight = m_nImageHeight / m_nRotationsCount;
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public void Render(Graphics g, Image img, boolean bFrameChanged) {
    if (_frameTimeout()) {
      if (bFrameChanged == true) {
        if (m_nFramesCount > 1)
          NextFrame();
      } else {
        if (m_nRotationsCount > 1)
          NextRotation();
      }
    }
    DirectRender(m_nX, m_nY, m_nCurFrame, m_nCurRotation, g, img);
  }

  /**
   * Estimate FrameDelay Info, If canUpdate return TRUE, the m_nCurFrame++, Render Sprite to Graphics g.
   * */
  public void Render(Graphics g, boolean bFrameChanged) {
    if (_frameTimeout()) {
      if (bFrameChanged == true) {
        if (m_nFramesCount > 1)
          NextFrame();
      } else {
        if (m_nRotationsCount > 1)
          NextRotation();
      }
    }
    DirectRender(m_nX, m_nY, m_nCurFrame, m_nCurRotation, g);
  }

  public void DirectRender(Graphics g) {
    DirectRender(m_nX, m_nY, m_nCurFrame, m_nCurRotation, g);
  }

  public void DirectRender(int x, int y, int nCurFrame, int nCurRotation, Graphics g, Image img) {
    //Backup Old Clip Info
    int nOldX = g.getClipX();
    int nOldY = g.getClipY();
    int nOldW = g.getClipWidth();
    int nOldH = g.getClipHeight();

    //Set New Clip Window, And Draw All Image to New Point
    g.setClip(x, y, m_nSpriteWidth, m_nSpriteHeight);
    g.drawImage(img,
                x - (m_nSpriteWidth * m_nCurFrame),
                y - (m_nSpriteHeight * m_nCurRotation),
                Graphics.TOP | Graphics.LEFT);

    //Restore Old Clip Info
    g.setClip(nOldX, nOldY, nOldW, nOldH);
  }

  public void DirectRender(int x, int y, int nCurFrame, int nCurRotation, Graphics g) {
    //Backup Old Clip Info
    int nOldX = g.getClipX();
    int nOldY = g.getClipY();
    int nOldW = g.getClipWidth();
    int nOldH = g.getClipHeight();

    //Set New Clip Window, And Draw All Image to New Point
    g.setClip(x, y, m_nSpriteWidth, m_nSpriteHeight);
    g.drawImage(m_imgSprite,
                x - (m_nSpriteWidth * m_nCurFrame),
                y - (m_nSpriteHeight * m_nCurRotation),
                Graphics.TOP | Graphics.LEFT);

    //Restore Old Clip Info
    g.setClip(nOldX, nOldY, nOldW, nOldH);
  }

  abstract public void DoAI(int nKey);

  protected boolean _frameTimeout() {
    if (IsPaused() == true) {
      return false;
    }

    long lNow = System.currentTimeMillis();
    if ((lNow - m_lLastTick) > m_nFrameDelay) {
      m_lLastTick = lNow;
      return true;
    }

    return false;
  }

  public Image getSpriteImage() {
    return m_imgSprite;
  }

  public void NextRotation() {
    if (IsPaused() == true) {
      return;
    }

    m_nCurRotation++;
    if (m_nCurRotation >= m_nRotationsCount) {
      m_nCurRotation = 0;
    }
  }

  public void NextFrame() {
    if (IsPaused() == true) {
      return;
    }

    m_nCurFrame++;
    if (m_nCurFrame >= m_nFramesCount) {
      m_nCurFrame = 0;
    }
  }

  public int getCurFrame() {
    return m_nCurFrame;
  }

  public int getCurRotation() {
    return m_nCurRotation;
  }

  public void setCurFrameRotation(int nFrame, int nRotation) {
    m_nCurFrame = nFrame;
    m_nCurRotation = nRotation;
  }

  public int getFramesCount() {
    return m_nFramesCount;
  }

  public int getRotationsCount() {
    return m_nRotationsCount;
  }

  public int getImageWidth() {
    return m_nImageWidth;
  }

  public int getImageHeight() {
    return m_nImageHeight;
  }

  public int getSpriteWidth() {
    return m_nSpriteWidth;
  }

  public int getSpriteHeight() {
    return m_nSpriteHeight;
  }

  public int getFramesDelay() {
    return m_nFrameDelay;
  }

  public void setFramesDelay(int nDelay) {
    m_nFrameDelay = nDelay;
  }

  public int getX() {
    return m_nX;
  }

  public int getY() {
    return m_nY;
  }

  public void setX(int x) {
    m_nX = x;
  }

  public void setY(int y) {
    m_nY = y;
  }

  public void setXY(int x, int y) {
    m_nX = x;
    m_nY = y;
  }

  public boolean IsPaused() {
    return m_bPaused;
  }

  public void Pause() {
    m_bPaused = true;
  }

  public void Resume() {
    m_bPaused = false;
  }
}

⌨️ 快捷键说明

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