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

📄 saveandload.java

📁 应用j2me技术开发的手机游戏
💻 JAVA
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.Font;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class SaveAndLoad {

	private static MainMidlet mid;
	private static RecordStore rs;
	private ByteArrayInputStream bin;
	private ByteArrayOutputStream bout;
	private DataInputStream din;
	private DataOutputStream dout;
	public static ListBox lbLoad = new ListBox(), lbSave = new ListBox();
	byte reid = 1;

	static {
		try {
			rs = RecordStore.openRecordStore("sl", true);
			for (int i = 1; i <= 3; i++) {
				rs.addRecord(null, 0, 0);
				lbLoad.addItem("无记录");
				lbSave.addItem("无记录");
			}
			lbLoad.setLineSpace(10);
			lbLoad.setBackground(false);
			lbLoad.setFontSize(Font.SIZE_LARGE);
			lbLoad.setBarColor(0xff5678, true);
			lbLoad.setBackground(true);

			lbSave.setLineSpace(10);
			lbSave.setFontSize(Font.SIZE_LARGE);
			lbSave.setBarColor(0xff5678, true);
			lbSave.setBackground(true);

		} catch (RecordStoreFullException e) {

			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {

			e.printStackTrace();
		} catch (RecordStoreException e) {

			e.printStackTrace();
		}
	}

	SaveAndLoad(MainMidlet m) {
		mid = m;
	}

	public void save(int id, Game game) {
		try {

			bout = new ByteArrayOutputStream();
			dout = new DataOutputStream(bout);
			System.out.println("gamelin.task0" + game.lin.task[0]);
			if (game.lin.task[0] != 1) {
				game.lin.firstLoad = true;

				game.lin.secondLoad = false;
			} else {
				game.lin.firstLoad = false;
				game.lin.secondLoad = true;
			}
			System.out.println("gamelin.load" + game.lin.firstLoad
					+ game.lin.secondLoad);
			dout.writeInt(game.GAMESTATE);
			dout.writeUTF(game.lin.aname);
			dout.writeInt(game.lin.exp);
			dout.writeInt(game.lin.feng);
			dout.writeInt(game.lin.friendfeina);

			dout.writeInt(game.lin.gold);
			dout.writeInt(game.lin.hdx);
			dout.writeInt(game.lin.hdy);
			dout.writeInt(game.lin.hx);
			dout.writeInt(game.lin.hy);

			dout.writeInt(game.lin.lev);
			dout.writeInt(game.lin.trait);
			dout.writeInt(game.lin.zmap);
			dout.writeInt(game.lin.diagState);
			dout.writeInt(game.lin.maxHp);
			dout.writeInt(game.lin.maxMp);
			dout.writeBoolean(game.lin.firstLoad);
			dout.writeBoolean(game.lin.secondLoad);
			dout.writeBoolean(game.lin.load);
			for (int i = 0; i < game.lin.atr.length; i++) {
				dout.writeInt(game.lin.atr[i]);
			}
			for (int i = 0; i < game.lin.arm.length; i++)
				dout.writeInt(game.lin.arm[i]);

			for (int i = 0; i < game.lin.item.length; i++) {

				dout.writeInt(game.lin.item[i]);
			}

			for (int i = 0; i < game.lin.skill.length; i++) {
				dout.writeInt(game.lin.skill[i]);
			}
			for (int i = 0; i < game.lin.task.length; i++) {
				dout.writeInt(game.lin.task[i]);
			}

			byte b[] = bout.toByteArray();
			rs.setRecord(id, b, 0, b.length);
			lbSave.replaceItem(id - 1, "姓名:" + game.lin.aname + "等级"
					+ game.lin.lev);
			lbLoad.replaceItem(id - 1, "姓名:" + game.lin.aname + "等级"
					+ game.lin.lev);

		} catch (RecordStoreFullException e) {

			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("kongzhizhen");
		}

	}

	public void load(int id, Game game) {// 把遇到的问题都留下来,好解决以后的,至于删除,另外复制一份就可以了;

		byte b[];
		try {
			rs = RecordStore.openRecordStore("sl", false);
			b = rs.getRecord(id);
			System.out.println("yunxingdaolezheli");
			if (b != null) {

				System.out.println("運行了load");
				bin = new ByteArrayInputStream(b);
				din = new DataInputStream(bin);
				game.GAMESTATE = din.readInt();
				game.GAMESTATE = Const.GAME_RUNNING;
				game.lin.aname = din.readUTF();
				game.lin.exp = din.readInt();
				game.lin.feng = din.readInt();
				game.lin.friendfeina = din.readInt();

				game.lin.gold = din.readInt();
				game.lin.hdx = din.readInt();
				game.lin.hdy = din.readInt();
				game.lin.hx = din.readInt();
				game.lin.hy = din.readInt();

				game.lin.lev = din.readInt();
				game.lin.trait = din.readInt();
				game.lin.zmap = din.readInt();
				game.lin.diagState = din.readInt();
				game.lin.maxHp = din.readInt();
				game.lin.maxMp = din.readInt();
				game.lin.firstLoad = din.readBoolean();
				game.lin.secondLoad = din.readBoolean();
				game.lin.load = din.readBoolean();
				System.out.println("gamelin.load" + game.lin.firstLoad
						+ game.lin.secondLoad);
				for (int i = 0; i < game.lin.atr.length; i++) {
					game.lin.atr[i] = din.readInt();
				}
				for (int i = 0; i < game.lin.arm.length; i++)
					game.lin.arm[i] = din.readInt();

				for (int i = 0; i < game.lin.item.length; i++) {

					game.lin.item[i] = din.readInt();
				}
				for (int i = 0; i < game.lin.skill.length; i++) {
					game.lin.skill[i] = din.readInt();
				}

				for (int i = 0; i < game.lin.task.length; i++) {
					game.lin.task[i] = din.readInt();
				}
				game.lin.load = true;
				mid.dis.setCurrent(game);
				game.start();// 现在读取后出现屏幕闪烁,明显是在刷新的屏幕造成的;也就是说建立了对象还在运行另一个程序快,同时运行了两次,start两次,已经解决;,并没有
				// 被消除
			}
		} catch (RecordStoreNotOpenException e) {

			e.printStackTrace();
		} catch (InvalidRecordIDException e) {

			e.printStackTrace();
		} catch (RecordStoreException e) {

			e.printStackTrace();
		} catch (IOException e) {

			System.out.println("cuowufashengle");
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

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