【发布时间】:2013-07-23 22:14:15
【问题描述】:
在 android 设备上安装并运行我的应用程序后,当我点击 highscore 时,如果它第一次运行我的问题是此代码,它应该发布“highscore:0”
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
Android 设备中似乎没有 system.DocumentsDirectory 我需要在写入之前创建文本文件拳头问题是我的路径我需要它来创建 myfile.text 那么什么可以替代 system.DocumentsDirectory?我不能使用 system.ResourceDirectory 导致它只可读不可写
这是用于我的 highscore.lua,如果用户在安装后玩游戏之前检查第一个高分
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
local file = io.open( path, "r" )
local savedData = file:read( "*n" )
if (savedData == nil) then
file=io.open(path,"w")
local newVal="0"
file:write(newVal)
file:flush()
local scoreText = display.newText("score: " .. newVal, 0, 0, "BorisBlackBloxx", 50)
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
else
local scoreText = display.newText("score: " .. savedData, 0, 0, "BorisBlackBloxx", 50)
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
end
如果用户第一次玩游戏,我的 game.lua 使用它
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
local reader = io.open( path, "r" )
local contents = reader:read("*n")
local file = io.open( path, "w" )
if (contents == nil) then
local walaVal="0"
file:write(walaVal)
file:flush()
else
file:write(contents)
file:flush()
end
【问题讨论】:
-
你的App有权限访问存储吗?