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

📄 scorestore.java

📁 关于j2me的宠物游戏!!!!!!!!!!! 希望打架提出意见
💻 JAVA
字号:
/*
 * ScoreStore.java
 *
 * Created on 2006年5月5日, 上午11:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package DogPet;


import java.io.PrintStream;
import java.util.*;
import javax.microedition.rms.RecordStore;

// Referenced classes of package com.webineti:
//            GameData, ScoreRecord

public class ScoreStore
{
    private RecordStore store;
    private int maxRecords;
    private Vector records;
    private long lowest;

    public void LoadData()
    {
        try
        {
            store = RecordStore.openRecordStore("/Pet", true);//********打开记录存储器*******
            byte s[] = store.getRecord(1);//******获得记录存储器的第一条记录的值******
            GameData.Name = new String(s);//用平台的缺省字符编码方式转换指定的字节数组生成一个新的 String 。
            long mood = 0L;//将心情值设置为0???????????????
            s = store.getRecord(2);
            try
            {
                GameData.Mood = Integer.parseInt(new String(s));
                s = store.getRecord(3);
                GameData.Health = Integer.parseInt(new String(s));
                s = store.getRecord(4);
                GameData.Capacity = Integer.parseInt(new String(s));
                s = store.getRecord(5);
                GameData.Intelligence = Integer.parseInt(new String(s));
                s = store.getRecord(6);
                GameData.Weight = Integer.parseInt(new String(s));
                s = store.getRecord(7);
                GameData.Years = Integer.parseInt(new String(s));
                s = store.getRecord(8);
                GameData.birthday = Long.parseLong(new String(s));
                Calendar cal = Calendar.getInstance();//Calendar 提供了一个类方法 getInstance,以获得该类型的一个通用的对象。
                                             //Calendar 的 getInstance 方法返回一个 GregorianCalendar 对象,该对象的时间域由当前的日期和时间初始化:
                Date ds = new Date(Long.parseLong(new String(s)));
                cal.setTime(ds);//设定当前时间为ds的时间
                GameData.Birthday = cal;
            }
            catch(NumberFormatException e) {
                System.out.println("11您在记录存储器中存储的记录格式有错误!!!");
            }
        }
        catch(Exception e) { 
            System.out.println("22您在记录存储器中存储的记录格式有错误!!!");
        }
    }

    public void startSavedata()  //开始新游戏时候记录存储器中的数值
    {
        try
        {
            store = RecordStore.openRecordStore("/Pet", true);
            ScoreRecord r = new ScoreRecord(); //创建一个新的数据记录类对象,用来存储新的数据记录
            String name = r.getUserName();//获得用户名
            String mood = "" + 0;
            String health = "" + 0;
            String capacity = "" + 0;
            String intelligence = "" + 0;
            String weight = "" + 0;
            String years = "" + 0;
            GameData.birthday = 0L;
            String birthday = "" + 1;
            if(store.getNumRecords() == 0)//判断记录存储器中的记录是否为空
            {
                store.addRecord(name.getBytes(), 0, name.length());
                store.addRecord(mood.getBytes(), 0, mood.length());
                store.addRecord(health.getBytes(), 0, health.length());
                store.addRecord(capacity.getBytes(), 0, capacity.length());
                store.addRecord(intelligence.getBytes(), 0, intelligence.length());
                store.addRecord(weight.getBytes(), 0, weight.length());
                store.addRecord(years.getBytes(), 0, years.length());
                store.addRecord(birthday.getBytes(), 0, years.length());
            } else
            {
                store.setRecord(1, name.getBytes(), 0, name.length());
                store.setRecord(2, mood.getBytes(), 0, mood.length());
                store.setRecord(3, health.getBytes(), 0, health.length());
                store.setRecord(4, capacity.getBytes(), 0, capacity.length());
                store.setRecord(5, intelligence.getBytes(), 0, intelligence.length());
                store.setRecord(6, weight.getBytes(), 0, weight.length());
                store.setRecord(7, years.getBytes(), 0, years.length());
                store.setRecord(8, birthday.getBytes(), 0, birthday.length());
            }
        }
        catch(Exception e) { }
    }

    public ScoreStore()
    {
        maxRecords = 10;
        records = new Vector();//Vector 类实现了可动态扩充的对象数组。类似数组,它包含的元素可通过数组下标来访问。但是,在 Vector 创建之后。Vector 可根据增加和删除元素的需要来扩大或缩小。 
        lowest = 0L;
    }

    public void Savedata()  //游戏销毁的时候  要保存的数据的操作方法
    {
        try
        {
            store = RecordStore.openRecordStore("/Pet", true);
            ScoreRecord r = new ScoreRecord();
            String name = r.getUserName();
            String mood = "" + r.getMood();
            String health = "" + r.getHealth();
            String capacity = "" + r.getCapacity();
            String intelligence = "" + r.getIntelligence();
            String weight = "" + r.getWeight();
            String years = "" + r.getYears();
            GameData.birthday = r.getBirthday().getTime().getTime();
            String birthday = "" + r.getBirthday().getTime().getTime();
            if(store.getNumRecords() == 0)
            {
                store.addRecord(name.getBytes(), 0, name.length());
                store.addRecord(mood.getBytes(), 0, mood.length());
                store.addRecord(health.getBytes(), 0, health.length());
                store.addRecord(capacity.getBytes(), 0, capacity.length());
                store.addRecord(intelligence.getBytes(), 0, intelligence.length());
                store.addRecord(weight.getBytes(), 0, weight.length());
                store.addRecord(years.getBytes(), 0, years.length());
                store.addRecord(birthday.getBytes(), 0, years.length());
            } else
            {
                store.setRecord(1, name.getBytes(), 0, name.length());
                store.setRecord(2, mood.getBytes(), 0, mood.length());
                store.setRecord(3, health.getBytes(), 0, health.length());
                store.setRecord(4, capacity.getBytes(), 0, capacity.length());
                store.setRecord(5, intelligence.getBytes(), 0, intelligence.length());
                store.setRecord(6, weight.getBytes(), 0, weight.length());
                store.setRecord(7, years.getBytes(), 0, years.length());
                store.setRecord(8, birthday.getBytes(), 0, birthday.length());
            }
        }
        catch(Exception e) { }
    }
}

⌨️ 快捷键说明

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