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

📄 settings.java

📁 一款用j2me做的手机游戏
💻 JAVA
字号:
import javax.microedition.rms.*;
import java.util.*;
import java.io.*;

public class Settings extends BaseRMS {
  private int difficulty = 1;
  private int autoFire = 1;

  public Settings(String rmsName) {
    super(rmsName);
  }

  public void loadSettings() throws Exception {
    try {
      // Will call either loadData() or createDefaultData()
      this.open();

      // Add any other neccessary processing, in this case there is none

      // Close
      if (this.getRecordStore() != null)
        this.close();
    } catch (Exception e) {
      throw new Exception("Error loading Settings" + e);
    }
  }

  public int getDifficulty() {
    return this.difficulty;
  }

  public int getAutoFire() {
    return this.autoFire;
  }

  public void updateSettings(int difficulty,int autoFire) throws Exception {
    try {
      // load current scores
      this.open();

      // update data
      this.difficulty = difficulty;
      this.autoFire = autoFire;

      // Update High Scores
      updateData();

      // close
      if (this.getRecordStore() != null)
        this.close();
    } catch (Exception e) {
      throw new Exception(this.getRMSName() + "::updateSettings::" + e);
    }
  }

  protected void loadData() throws Exception {
    try {
        byte[] record = this.getRecordStore().getRecord(1);
        DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record,0,record.length));
        difficulty = istream.readInt();
        autoFire = istream.readInt();
    } catch (Exception e) {
      throw new Exception (this.getRMSName() + "::loadData::" + e);
    }
  }

  protected void createDefaultData() throws Exception {
    try {
      ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
      DataOutputStream ostream = new DataOutputStream(bstream);
      ostream.writeInt(difficulty);
      ostream.writeInt(autoFire);
      ostream.flush();
      ostream.close();
      byte[] record = bstream.toByteArray();
      this.getRecordStore().addRecord(record, 0, record.length);
    } catch (Exception e) {
      throw new Exception(this.getRMSName() + "::createDefaultData::" + e);
    }
  }

  protected void updateData() throws Exception {
    try {
      ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
      DataOutputStream ostream = new DataOutputStream(bstream);
      ostream.writeInt(difficulty);
      ostream.writeInt(autoFire);
      ostream.flush();
      ostream.close();
      byte[] record = bstream.toByteArray();
      this.getRecordStore().setRecord(1, record, 0, record.length);
    } catch(Exception e) {
      throw new Exception(this.getRMSName() + "::updateData::" + e);
    }
  }
}

⌨️ 快捷键说明

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