在设备的本地存储上创建一个Android保存文件? [英] Creating an Android save file on the local storage of the device?

查看:121
本文介绍了在设备的本地存储上创建一个Android保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用Libgdx进行游戏,并且我想在用户手机的本地存储上创建一个保存文件,我想知道如何去做这件事。我读过的一些文件如下: https:// developer.android.com/guide/topics/data/data-storage.html#filesInternal 一直都很模糊,我也不太明白发生了什么,或者可以让我的例子在我自己的游戏中运行。 Libgdx本身是否有这个功能?

I'm using Libgdx for a game on Android, and i want to create a save file on the local storage of the user's phone, and i'm wondering how would i go about doing that. Some of the documents I have read such as here: https://developer.android.com/guide/topics/data/data-storage.html#filesInternal have been quiet vague, and I don't really understand what is going on, or can make the example work in my own game. Is there a feature for this in Libgdx itself?

推荐答案

在libgdx中用于存储我们使用Preference的数据。这有助于将数据存储在设备本身中。在这里我将给你一个例子,用于将数据存储在deveice内存中。

in libgdx for storing the data we uses the "Preference". which is help to store the data in device itself. here i will give you an example for storing the data in deveice memory.

           myGame extends ApplicationAdapter{
           public static int count;
           public static bitmapFont font;
           public SpriteBatch batch;
           public static Preferences prefs;  
           public void create()
           {
           batch=new SpriteBatch();
           font=new font(Gdx.files.internla("myFont.png"));
           public static Preferences prefs;
        // this is for setting up the storage for the current project 
           prefs = Gdx.app.getPreferences("myGame");
            }
           public void incrementCount()
          {
          count++;
          if(count>100){
         // here "countValue" is the key(which is the reference to the data)  and "count" is the vlaue(the data). the value is stored in key value method.

          prefs.putInteger("countValue", count);

         // is used to flush the data in to the storage
           prefs.flush();
             }
           public void render()
            {
         // getting the stored value from the preference
            pref.getInteger("countValue",countValue);
            batch.begin();
            batch.draw(font,countValue+"stored value",0,0)
            batch.end()
               }

         // this is the simple way to store the data into the device memory . it will work in both the android and the IOS

这篇关于在设备的本地存储上创建一个Android保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆