如何保存一个变量到Android使用的Cocos2D-JS? [英] How to save a variable to Android using Cocos2D-JS?

查看:155
本文介绍了如何保存一个变量到Android使用的Cocos2D-JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存高分整数到用户的Andr​​oid系统,以便它可以在所有游戏体验依然存在。

I am trying to save a high score integer to the user's Android system, so that it can persist throughout all game-play experience.

我已阅读,使用的Cocos2D-X可以使用 NSUserDefaults的但这似乎不是可用的Cocos2D-JS API中的。

I have read that using Cocos2D-X one can use NSUserDefaults but this doesn't seem to be available in the Cocos2D-JS API at all.

任何人有任何这方面的经验,是否有任何其他有效的方式如何解决这个问题呢?

Anyone has any experience with this, is there any other efficient way how to tackle this issue?

推荐答案

在使用的Cocos2D-JS编译原生应用程序,你可以简单地使用的localStorage 就像如果你是在浏览器中运行的游戏:D

When using Cocos2D-JS for compiling native apps, you can simply use localStorage just like if you were running your game in the browser :D

例如:

//Handle for quick access to Cocos2D's implementation of Local Storage:
var ls = cc.sys.localStorage;

var value = "foo";
var key  = "bar";

//This should save value "foo" on key "bar" on Local Storage
ls.setItem(key, value);

//This should read the content associated with key "bar" from Local Storage:
var data = ls.getItem(key);

cc.log(data); //Should output "foo" to the console.

//This should remove the contents of key "bar" from Local Storage:
ls.removeItem(key);

//This should print "null"
data = ls.getItem(key);
cc.log(data);

这篇关于如何保存一个变量到Android使用的Cocos2D-JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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