Libgdx学习记录14——数据保存Preferences,XmlReader

时间:2024-06-21 15:06:32

在游戏过程过程中,常常需要对某些数据进行保存,以确保下次运行时能够正确读取。如游戏中的金币、道具、游戏分数、已通过的关卡等。

Libgdx中常用的数据保存方式有Preferences和XmlReader。

1. Perferences

Preferences常用来保存一些较短的、使用较频繁的值。

put存入值,存完之后需要flush。

实例:

         Preferences preferences = Gdx.app.getPreferences( "pre1.test" );
preferences.putString( "name", "Kitty" );
preferences.putBoolean( "visible", true );
preferences.putInteger( "age", );
preferences.flush(); String strName1 = preferences.getString( "name" );
boolean isVisible = preferences.getBoolean( "visible" );
int age1 = preferences.getInteger( "age" );

2. XmlReader

XmlReader能够读取xml文件,不过此时只能读取,不能写入。

使用Element.get()获取child或者元素的值,示例:

         try{
XmlReader xmlReader = new XmlReader( );
Element eleRoot = xmlReader.parse( Gdx.files.internal( "xml/1.xml" ) );
Element eleStu = eleRoot.getChildByName( "student" ); String strName2 = eleStu.get( "name" );
boolean isMale = eleStu.getBoolean( "male" );
int age2 = eleStu.getInt( "age" );
strShow2 = strName2 + " " + isMale + " " + age2;
}
catch(IOException e){
e.printStackTrace();
}

3. XmlWriter

XmlWriter能够将Xml的值写入到StringWriter中,然后写入到xml文件中进行保存。

其中.enimate()方法创建子元素,.text()方法插入元素值,.attribute()方法插入元素属性。

示例:

         try{
stringWriter = new StringWriter();
//stringWriter.append( "" );
XmlWriter xmlWriter = new XmlWriter( stringWriter );
xmlWriter.element("information")
.element( "person" ).attribute( "id", "0201" )
.element("name").text("Nacy").pop()
.element("hobby").text("basketball").pop()
.element("age").text("34").pop()
.pop()
.pop();
xmlWriter.close();
FileHandle file = Gdx.files.local( "set.xml" );
file.writeString( stringWriter.toString(), false );
System.out.println( stringWriter.toString() );
}
catch(IOException e){
e.printStackTrace();
}

完整代码:

 package com.fxb.newtest;

 import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;
import com.badlogic.gdx.utils.XmlWriter; public class Lib013_XmlReader1 extends ApplicationAdapter{ //XmlReader reader;
//Element element;
BitmapFont font;
String strShow1, strShow2;
SpriteBatch batch;
StringWriter stringWriter;
String configFileName = "xml/3.xml"; @Override
public void create() {
// TODO Auto-generated method stub
super.create(); font = new BitmapFont();
font.setColor( Color.BLACK );
batch = new SpriteBatch(); Preferences preferences = Gdx.app.getPreferences( "pre1.test" );
preferences.putString( "name", "Kitty" );
preferences.putBoolean( "visible", true );
preferences.putInteger( "age", 25 );
preferences.flush(); String strName1 = preferences.getString( "name" );
boolean isVisible = preferences.getBoolean( "visible" );
int age1 = preferences.getInteger( "age" );
strShow1 = strName1 + " " + isVisible + " " + age1; try{
XmlReader xmlReader = new XmlReader( );
Element eleRoot = xmlReader.parse( Gdx.files.internal( "xml/1.xml" ) );
Element eleStu = eleRoot.getChildByName( "student" ); String strName2 = eleStu.get( "name" );
boolean isMale = eleStu.getBoolean( "male" );
int age2 = eleStu.getInt( "age" );
strShow2 = strName2 + " " + isMale + " " + age2;
}
catch(IOException e){
e.printStackTrace();
} try{
stringWriter = new StringWriter();
//stringWriter.append( "" );
XmlWriter xmlWriter = new XmlWriter( stringWriter );
xmlWriter.element("information")
.element( "person" ).attribute( "id", "0201" )
.element("name").text("Nacy").pop()
.element("hobby").text("basketball").pop()
.element("age").text("34").pop()
.pop()
.pop();
xmlWriter.close();
FileHandle file = Gdx.files.local( "set.xml" );
file.writeString( stringWriter.toString(), false );
System.out.println( stringWriter.toString() );
}
catch(IOException e){
e.printStackTrace();
} } @Override
public void render() {
// TODO Auto-generated method stub
super.render();
Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); batch.begin();
font.draw( batch, strShow1, 100, 150 );
font.draw( batch, strShow2, 100, 100 );
font.drawMultiLine( batch, stringWriter.toString(), 100, 300 );
batch.end(); } @Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
} }

运行结果:

Libgdx学习记录14——数据保存Preferences,XmlReader