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

📄 actionframe.java

📁 JAVAME手机RPG游戏设计最终演示代码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import java.io.*;
public class ActionFrame {
  private int m_iTime; //显示时间
  private byte m_Num; //有几个切片
  private byte[] m_imgID; //图片ID
  private int[] m_iX; //切片相对于帧坐标
  private int[] m_iY;
  private int[] m_iCX; //切片的裁截矩形
  private int[] m_iCY;
  private int[] m_iCW;
  private int[] m_iCH;
  private int[] m_iHitRect = new int[4]; //碰撞矩形 包含了起始点和宽高4个值
  private int[] m_iAttackRect = new int[4]; //攻击矩形 包含了起始点和宽高4个值
  private long lStartTime; //开始画的时间点
  private boolean bStart; //是否开始绘制帧
  public ActionFrame() {
  }

  /**
   * Copy帧数据
   * @param rf ActionFrame
   */
  public void copyFrame(ActionFrame rf) {
    m_Num = rf.m_Num; //Copy切片数
    m_imgID = new byte[m_Num]; //创建数组
    m_iX = new int[m_Num];
    m_iY = new int[m_Num];
    m_iCX = new int[m_Num];
    m_iCY = new int[m_Num];
    m_iCW = new int[m_Num];
    m_iCH = new int[m_Num];
    for (int i = 0; i < m_Num; i++) {
      m_imgID[i] = rf.m_imgID[i]; //图片ID
      m_iX[i] = rf.m_iX[i]; //绘制位置
      m_iY[i] = rf.m_iY[i];
      m_iCX[i] = rf.m_iCX[i]; //资源图的哪部分
      m_iCY[i] = rf.m_iCY[i];
      m_iCW[i] = rf.m_iCW[i];
      m_iCH[i] = rf.m_iCH[i];
    }
    for (int i = 0; i < m_iHitRect.length; i++) {//copy碰撞矩形
      m_iHitRect[i] = rf.m_iHitRect[i];
    }
    for (int i = 0; i < m_iHitRect.length; i++) {//copy攻击矩形
      m_iAttackRect[i] = rf.m_iAttackRect[i];
    }
  }

  /**
   * 取得帧的数据
   * @param din DataInputStream
   */
  public void loadFrameData(DataInputStream din) { //载入帧数据
    m_iTime = 100; //定义帧显示时间
    try {
      m_Num = din.readByte(); //从输入流读入切片的数量
      din.readInt(); //读取帧相对于帧原点的X轴偏移量
      din.readInt(); //读取帧相对于帧原点的Y轴偏移量
      m_imgID = new byte[m_Num]; //创建数组
      m_iX = new int[m_Num];
      m_iY = new int[m_Num];
      m_iCX = new int[m_Num];
      m_iCY = new int[m_Num];
      m_iCW = new int[m_Num];
      m_iCH = new int[m_Num];
      for (int i = 0; i < m_Num; i++) {
        m_imgID[i] = din.readByte();
        m_iX[i] = din.readInt(); //切片相对于帧坐标
        m_iY[i] = din.readInt();
        m_iCX[i] = din.readInt(); //切片的裁截矩形
        m_iCY[i] = din.readInt();
        m_iCW[i] = din.readInt();
        m_iCH[i] = din.readInt();
      }
      int hitRectNum = din.readByte(); //碰撞矩形的数量 在本书中皆为1不作考虑
      if (hitRectNum > 0) {
        for (int j = 0; j < m_iHitRect.length; j++) {
          m_iHitRect[j] = din.readInt(); //读取碰撞矩形的数值
        }
      }
      if (din.readByte() > 0) { //攻击矩形的数量
        for (int j = 0; j < m_iAttackRect.length; j++) {
          m_iAttackRect[j] = din.readInt(); //读取碰撞矩形的数值
        }
      }
      din.readByte(); //受攻击矩形的数量 在本节中皆为0不作考虑
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  /**
   * 绘制动作帧
   * @param g Graphics
   * @param x int  帧的屏幕坐标
   * @param y int
   */
  public boolean drawActionFrame(Graphics g, int x, int y, int type) { //绘制帧
    if (!bStart) { //
      bStart = true; //把开始状态设为true
      lStartTime = System.currentTimeMillis(); //记录下开始画的时间
    }
    if (type == 0) { //不镜像
      for (int i = 0; i < m_Num; i++) { //绘制所有切片
        g.setClip(x + m_iX[i], y + m_iY[i], m_iCW[i], m_iCH[i]);
        g.drawImage(Data.imgRole[m_imgID[i]],
                    x + m_iX[i] - m_iCX[i], y + m_iY[i] - m_iCY[i], 0);
      }
    }
    else { //镜像
      for (int i = 0; i < m_Num; i++) { //绘制所有切片
        g.setClip(x - (m_iX[i] + m_iCW[i]), y + m_iY[i], //根据中心对称点反转所以要减去
                  m_iCW[i], m_iCH[i]);
        g.drawRegion(Data.imgRole[m_imgID[i]], //用Midp2.0的方法来绘制镜像
                     m_iCX[i], m_iCY[i], m_iCW[i], m_iCH[i],
                     2, x - (m_iX[i] + m_iCW[i]), y + m_iY[i], 0);
      }
    }


    g.setColor(0x0000ff);
    g.setClip(0, 0, Data.ScreenWidth, Data.ScreenHeight);
    g.drawRect(x +m_iHitRect[0]//- (m_iHitRect[0] + m_iHitRect[2])
               ,
               y + m_iHitRect[1]
               , m_iHitRect[2], m_iHitRect[3]);


    if (System.currentTimeMillis() - lStartTime > m_iTime) { //判断该帧绘制时间是否大于需要的绘制时间
      bStart = false; //绘制完成后 还原开始绘制的标志
      return true; //返回true表示该帧的显示时间已经达到,可以切换到下帧
    }
    return false;
  }

  public int[] getM_iHitRect() { //取得碰撞区域
    return m_iHitRect;
  }

  public int[] getM_iAttackRect() { //取得攻击区域
    return m_iAttackRect;
  }
}

⌨️ 快捷键说明

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