Cocos2d-x Android环境中Lua脚本的io操作

woxmh 2019-12-30

Cocos2d-x Android环境中Lua脚本的io操作

因为android的安装包安装之后,是以压缩文件的形式存储.
故在lua脚本中,直接使用io操作(如:io.open等)是无法找到文件的(ios和windows平台无此问题).

原理是在lua中调用CCString:createWithContentsOfFile方法,使用c++代码去打开文件。具体代码:
local str = CCString:createWithContentsOfFile(CCFileUtils:sharedFileUtils():fullPathForFilename("config/config_skill.json")):getCString();

版本迭代之后, 代码如下:

local path = 'res/config.json'
local fp = cc.FileUtils:getInstance():fullPathForFilename(path)
local content = CCString:createWithContentsOfFile(fp):getCString()

相关推荐