在游戏过程过程中,常常需要对某些数据进行保存,以确保下次运行时能够正确读取。如游戏中的金币、道具、游戏分数、已通过的关卡等。
Libgdx中常用的数据保存方式有Preferences和XmlReader。
1. Perferences
Preferences常用来保存一些较短的、使用较频繁的值。
put存入值,存完之后需要flush。
实例:
1 Preferences preferences = Gdx.app.getPreferences( "pre1.test" ); 2 preferences.putString( "name", "Kitty" ); 3 preferences.putBoolean( "visible", true ); 4 preferences.putInteger( "age", 25 ); 5 preferences.flush(); 6 7 String strName1 = preferences.getString( "name" ); 8 boolean isVisible = preferences.getBoolean( "visible" ); 9 int age1 = preferences.getInteger( "age" );
2. XmlReader
XmlReader能够读取xml文件,不过此时只能读取,不能写入。
使用Element.get()获取child或者元素的值,示例:
1 try{ 2 XmlReader xmlReader = new XmlReader( ); 3 Element eleRoot = xmlReader.parse( Gdx.files.internal( "xml/1.xml" ) ); 4 Element eleStu = eleRoot.getChildByName( "student" ); 5 6 String strName2 = eleStu.get( "name" ); 7 boolean isMale = eleStu.getBoolean( "male" ); 8 int age2 = eleStu.getInt( "age" ); 9 strShow2 = strName2 + " " + isMale + " " + age2; 10 } 11 catch(IOException e){ 12 e.printStackTrace(); 13 }
3. XmlWriter
XmlWriter能够将Xml的值写入到StringWriter中,然后写入到xml文件中进行保存。
其中.enimate()方法创建子元素,.text()方法插入元素值,.attribute()方法插入元素属性。
示例:
1 try{ 2 stringWriter = new StringWriter(); 3 //stringWriter.append( "" ); 4 XmlWriter xmlWriter = new XmlWriter( stringWriter ); 5 xmlWriter.element("information") 6 .element( "person" ).attribute( "id", "0201" ) 7 .element("name").text("Nacy").pop() 8 .element("hobby").text("basketball").pop() 9 .element("age").text("34").pop() 10 .pop() 11 .pop(); 12 xmlWriter.close(); 13 FileHandle file = Gdx.files.local( "set.xml" ); 14 file.writeString( stringWriter.toString(), false ); 15 System.out.println( stringWriter.toString() ); 16 } 17 catch(IOException e){ 18 e.printStackTrace(); 19 }
完整代码:
1 package com.fxb.newtest; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.StringWriter; 6 7 import com.badlogic.gdx.ApplicationAdapter; 8 import com.badlogic.gdx.Gdx; 9 import com.badlogic.gdx.Preferences; 10 import com.badlogic.gdx.files.FileHandle; 11 import com.badlogic.gdx.graphics.Color; 12 import com.badlogic.gdx.graphics.GL10; 13 import com.badlogic.gdx.graphics.g2d.BitmapFont; 14 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 15 import com.badlogic.gdx.utils.XmlReader; 16 import com.badlogic.gdx.utils.XmlReader.Element; 17 import com.badlogic.gdx.utils.XmlWriter; 18 19 public class Lib013_XmlReader1 extends ApplicationAdapter{ 20 21 //XmlReader reader; 22 //Element element; 23 BitmapFont font; 24 String strShow1, strShow2; 25 SpriteBatch batch; 26 StringWriter stringWriter; 27 String configFileName = "xml/3.xml"; 28 29 @Override 30 public void create() { 31 // TODO Auto-generated method stub 32 super.create(); 33 34 font = new BitmapFont(); 35 font.setColor( Color.BLACK ); 36 batch = new SpriteBatch(); 37 38 Preferences preferences = Gdx.app.getPreferences( "pre1.test" ); 39 preferences.putString( "name", "Kitty" ); 40 preferences.putBoolean( "visible", true ); 41 preferences.putInteger( "age", 25 ); 42 preferences.flush(); 43 44 String strName1 = preferences.getString( "name" ); 45 boolean isVisible = preferences.getBoolean( "visible" ); 46 int age1 = preferences.getInteger( "age" ); 47 strShow1 = strName1 + " " + isVisible + " " + age1; 48 49 50 51 try{ 52 XmlReader xmlReader = new XmlReader( ); 53 Element eleRoot = xmlReader.parse( Gdx.files.internal( "xml/1.xml" ) ); 54 Element eleStu = eleRoot.getChildByName( "student" ); 55 56 String strName2 = eleStu.get( "name" ); 57 boolean isMale = eleStu.getBoolean( "male" ); 58 int age2 = eleStu.getInt( "age" ); 59 strShow2 = strName2 + " " + isMale + " " + age2; 60 } 61 catch(IOException e){ 62 e.printStackTrace(); 63 } 64 65 66 67 try{ 68 stringWriter = new StringWriter(); 69 //stringWriter.append( "" ); 70 XmlWriter xmlWriter = new XmlWriter( stringWriter ); 71 xmlWriter.element("information") 72 .element( "person" ).attribute( "id", "0201" ) 73 .element("name").text("Nacy").pop() 74 .element("hobby").text("basketball").pop() 75 .element("age").text("34").pop() 76 .pop() 77 .pop(); 78 xmlWriter.close(); 79 FileHandle file = Gdx.files.local( "set.xml" ); 80 file.writeString( stringWriter.toString(), false ); 81 System.out.println( stringWriter.toString() ); 82 } 83 catch(IOException e){ 84 e.printStackTrace(); 85 } 86 87 } 88 89 @Override 90 public void render() { 91 // TODO Auto-generated method stub 92 super.render(); 93 Gdx.gl.glClearColor( 1, 1, 1, 1 ); 94 Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); 95 96 batch.begin(); 97 font.draw( batch, strShow1, 100, 150 ); 98 font.draw( batch, strShow2, 100, 100 ); 99 font.drawMultiLine( batch, stringWriter.toString(), 100, 300 ); 100 batch.end(); 101 102 } 103 104 @Override 105 public void dispose() { 106 // TODO Auto-generated method stub 107 super.dispose(); 108 } 109 110 }
运行结果: