1

lua ファイルを動的にロードすることは可能ですか? たとえば、次のレベルをロードしますか?

背景: * ストーリーボード ベースの小さなゲームを用意する * レベルごとに異なるストーリーボード ファイルを作成する予定でしたが、ダイナミクス/コードはまったく同じなので、レベルの背景/オブジェクトを別のファイルから動的にロードすることにしました。たとえば、level_1.lua、level_2.lua などを使用できます。これらのファイル内で、(lua で) バックグラウンド/インタラクション オブジェクトなどのセット全体を作成し、メインのゲーム ストーリーボード ファイルに 1 つの表示オブジェクトとして戻すことができます * 機能しました「require level_1」で問題ありませんが、これを動的にしようとすると、問題が発生する可能性があります

それが不可能な場合、何か提案はありますか?

4

3 に答える 3

2

Corona SDK does let you require modules when needed, but this is different that dynamically loading modules at run time. Things loaded with the require statement are compiled at build time. You cannot later, download a .lua file and included it. Apple specifically prohibits this behavior.

But if your end goal is to follow the DRY principle (Don't Repeat Yourself), and have one set of code instead of duplicating it a bunch of times, if you can either one really big table with all your level data it in, or you can save each individual's data out to JSON formatted text files, and then read them back in on a level by level basis. You can't have executable code or formulas in there, but you can have image names, x, Y coordinates, physics properties, etc.

于 2013-01-06T02:49:41.980 に答える
2

loadfile() がコロナで機能しない場合は、機能するため、おそらく require を使用できます

例えば

if level == 1 then
  game = require "level1"
else
  game = require "level2"
end

http://www.lua.org/pil/8.1.htmlのどこでも require を使用できると思います

Lua は、require と呼ばれる、ライ​​ブラリをロードして実行するための高レベルの関数を提供します。おおよそ、require は dofile と同じ働きをしますが、2 つの重要な違いがあります。まず、パス内のファイルを検索する必要があります。次に、作業の重複を避けるために、ファイルが既に実行されているかどうかを制御する必要があります。これらの機能により、require はライブラリをロードするために Lua で推奨される関数です。

于 2013-01-03T04:52:34.550 に答える