在Corona SDK中使用子目录的方法

来源:互联网 发布:知乎 正义联盟 编辑:程序博客网 时间:2024/06/06 02:50

原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: 在Corona SDK中使用子目录的方法

如果你在用Corona SDK,那你一定对他的代码和资源组织方式颇有微辞。所有的代码和资源文件必须全部放在工程的根目录下,如果只是写一个简单的sample还好,当要处理几个几十个关卡,管理成百上千的图片和音乐资源文件,还有一堆的lua脚本文件的时候,这简直就像是一场噩梦。

 

在模拟器上支持子目录,理由很简单,当需要使用Corona的服务器来编译app时,只会将工程根目录下的文件发到服务器上去编译。很奇怪的是这个问题在很早的时候就被开发者所诟病,但是Corona开发者一直没有试图去改进。当然,在第三方工具Project Manager里面倒是提供了分类管理的功能,而且最新的Corona安装文件将30天试用期的Project Manager也包含在了里面。如果把这最基本的功能也作为一项服务来卖的话,实在是有点无语!

 

好了,抱怨的话多说无益,还是看看如何来解决这个问题吧。

 

在Code Exchange上终于有人提供了一个解决方案,Subfolders for Lua, audio, xml and other files. Keep sanity,7月17日由Lerg提供。

 

解决的方法也比较巧妙,在模拟器上开发时按照你希望的目录结构来进行,当需要提交到Corona Server去编译的时候使用脚本生成build目录,按照Corona的要求把代码和资源都放在这一个目录下。当然,因为lua代码编写的时候是分目录的,所以对于require方法也需要做一点修改,只需要把原来的xxx/xxx/xxx替换为xxx_xxx_xxx即可,很简单,但却非常的实用。

 

build目录生成的脚本使用python编写,代码如下:

 

#!/usr/bin/env python# -*- coding: utf-8 -*-"""prepare_build.pyTool for keeping Lua and other files in subfoldersAuthor: LergRelease date: 2011-07-18Version: 1.0License: MITUSAGE:    Modify original_path and build_path to your needs and then simply run in a    command line:    python prepare_build.py    You must be in the parent folder of your game's folder.    Filenames extensions which are moved are defined in a list below.I can be found on the corona IRC channel."""original_path = 'your_app_folder'build_path = 'build/'import os, shutilif os.path.isdir(build_path):    shutil.rmtree(build_path)os.mkdir(build_path)for root, dirs, files in os.walk(original_path):    for file in files:        if file[0] != '.':            filename = os.path.join(root, file)            inner = filename.split('/', 1)[1]            ext = filename.rsplit('.', 1)            if len(ext) == 2 and ext[1] in ['lua', 'mp3', 'wav', 'xml']:  # <- put other extensions here                inner = inner.replace('/', '_')            outfilename = build_path + inner            os.path.dirname(outfilename)            dir = os.path.dirname(outfilename)            if not os.path.isdir(dir):                os.makedirs(dir)            shutil.copyfile(filename, outfilename)

require方法的修改只需要在main.lua中定义即可,在main.lua的最前面定义自己的require方法,如下:

-- Override requireif system.getInfo('environment') ~= 'simulator' then    local old_require = _G.require    _G.require = function (path)        local path = path:gsub('/', '_')        return old_require(path)    end    isSimulator = falseelse    isSimulator = trueend

 

同样的,在所有加载资源文件的地方也需要采用类似的方法:

 

if not isSimulator then    filename = filename:gsub('/', '_')end

 

也就是把文件名做一次转换。

原文地址:http://developer.anscamobile.com/code/subfolders-lua-audio-xml-and-other-files-keep-sanity

原创文章,转载请注明: 转载自All-iPad.net

本文链接地址: 在Corona SDK中使用子目录的方法

原创粉丝点击