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

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

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

 

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

 

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

 

在Code Exchange上终于有人提供了一个解决方案, 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.py

Tool for keeping Lua and other files in subfolders

Author: Lerg
Release date: 2011-07-18
Version: 1.0
License: MIT

USAGE:
    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  of your game's .
    Filenames extensions which are moved are defined in a list below.

I can be found on the  IRC channel.
"""

original_path = 'your_app_'
build_path = 'build/'

import os, shutil

if 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 require
if system.getInfo('environment') ~= 'simulator' then
    local old_require = _G.require
    _G.require = function (path)
        local path = path:gsub('/', '_')
        return old_require(path)
    end
    isSimulator = false
else
    isSimulator = true
end

 

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

 

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中使用子目录的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值