我正在尝试加入sqlite支持来保存分数和一些标志。我需要打开数据库,如果它存在,然后初始化基于数据库的游戏值。如果db不存在,我需要创建/初始化它。下面的代码可以编译,但由于未知原因而崩溃。
package mygame;
<snip imports>
import sys.db.Types;
class ScoreDB extends sys.db.Object {
public var id : SId;
public var dbscore1 : SInt;
public var dbsound : SInt;
public var dbscore2 : SInt;
}
class mygame extends Sprite {
<snip var defines>
public function new () {
// start sqlite code
sys.db.Manager.initialize();
// db does exist
// then read values
// currentScore = score1.dbscore1;
// doSound = score1.dbsound;
// doScore = score1.dbscore2;
// db does not exist:
var cnx = sys.db.Sqlite.open("mybase.db");
sys.db.Manager.cnx = cnx;
sys.db.TableCreate.create(ScoreDB.manager);
var score1 = new ScoreDB();
score1.id = 0;
score1.dbscore1 = 0;
score1.dbsound = 0;
score1.dbscore2 = 0;
score1.insert();
currentScore = 0;
doSound = 0;
doScore = 0;
cnx.close();
// end sqlite code
super ();
initialize ();
construct ();
newGame ();
}发布于 2014-05-13 20:49:35
实际上我刚刚解决了同样的问题。
问题是,这个应用程序本质上是一个.zip文件,不能编辑。您需要在应用程序存储目录中创建(并在以后访问) DB。
要访问该目录,请使用以下代码:
private static var localStoragePath:String = openfl.utils.SystemPath.applicationStorageDirectory;有一个已知的错误,集成开发环境没有显示SystemPath类,但请不要介意,它会顺利编译的。之后,您可以使用常用工具来读写目录、创建新文件夹等。
这里有一个快速测试,以确保它可以工作并且不会崩溃:
// creating a test folder
sys.FileSystem.createDirectory(openfl.utils.SystemPath.applicationStorageDirectory + "/testFolder");
var form:TextFormat = new TextFormat(null, 22, 0xFFFFFF);
// getting contents of the storage dir
var arr = sys.FileSystem.readDirectory(openfl.utils.SystemPath.applicationStorageDirectory);
var tf:TextField = new TextField();
tf.defaultTextFormat = form;
tf.width = Lib.current.stage.stageWidth;
tf.height = Lib.current.stage.stageHeight;
tf.multiline = true;
tf.wordWrap = true;
tf.text = arr.toString();
addChild(tf);正如您将看到的,新创建的文件夹就在那里。您可以删除创建文件夹的行,您将看到它是安全的。
哦,别忘了在XML文件中添加Android权限:
<android permission="android.permission.WRITE_EXTERNAL_STORAGE"/>发布于 2014-05-21 22:19:25
据我所知,openfl中没有sqlite定义。而您使用的只是普通的cpp目标定义。我想问题是它们不能在android上工作。更重要的是:我非常确定api的定义是正确的,但它试图加载一个错误的名称的dll,这可能会杀死你的应用程序,甚至不放出一个错误。尝试查看实现(简短且易于理解),并更改dll名称。
https://stackoverflow.com/questions/23461509
复制相似问题