在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中使用子目录的方法
- 在Corona SDK中使用子目录的方法
- Corona SDK的优与劣
- Corona SDK的优与劣
- 在SDK中使用GDI+出现的编译问题解决方法
- corona new button(按钮的使用)
- Android SDK中相关子目录的功能说明
- Tomcat中设置站点子目录的方法
- XCODE6中使用iOS7 SDK的方法
- XCODE6中使用iOS7 SDK的方法
- Xcode6中使用iOS7 SDK的方法
- XCODE6中使用iOS7 SDK的方法
- C#中 dll文件放在子目录中的方法
- python中import子目录module中py文件的方法
- 在sdk中使用进度条
- 在 SDK 中使用 CString
- 在sdk中使用进度条
- 在eclipse中查看sdk源代码的方法
- 在xilinx SDK中查询API函数的方法
- Android 开发者必备的十个工具(下)
- poolman数据库连接池
- jquery常用的1000个插件
- Nutch-0.9源代码:Crawl类整体分析
- MR框架结构
- 在Corona SDK中使用子目录的方法
- Binder学习三 MediaServer -- 实例
- Item:明智而审慎的使用pirvate继承
- hdu 3938 Portal
- 问题1:Project has no default.properties file! Edit the project properties to set one.
- 文件操作:系统调用和C库函数的区别
- OAuth授权的Java实现详解
- 图的算法演示
- 揭开正则表达式的神秘面纱