游戏逻辑文件

发布于 2024-12-12 01:21:45 字数 1025 浏览 0 评论 0原文

我正在为移动设备创建我的第一个游戏,并使用 Corona 以 Lua 脚本语言来完成它。

我听说在不同的文件中创建每个级别时,将所有保持不变的内容放入一个逻辑文件中并将其加载到每个级别文件中会更节省内存。该文件中应该包含什么类型的内容、函数、精灵等?

另外,如果我的所有变量都是本地变量,如何将其加载到不同的文件中? 我尝试过需要“逻辑” - 我的文件名包含我的所有函数,但是我不确定如何将写入其中的函数“激活”,因为目前它们还没有。

如果您需要我发布任何代码,我很乐意这样做。 感谢您的阅读!


更新

感谢您提供表格的想法,我现在已将所有函数存储在一个表格中,它看起来像这样:

-- Logic File 
local functionTable = {}
functionTable[1] = onTouch,
functionTable[2] = physicsStart,
functionTable[3] = onComplete,
functionTable[4] = winCondition

但是我正在努力寻找一种在关卡文件中调用这些函数的方法,这是我尝试过的方法,但不起作用,您知道有什么方法可以改进吗?

-- Level File
local logic = require "logic"
logic.functionTable[1]
logic.functionTable[2]
logic.functionTable[3]
logic.functionTable[4]

当您说存储图像名称或路径时,假设我有一个名为 red_apple 的图像位于项目根目录的“graphics”文件夹中;以下说法正确吗?

local imagePath = {}
imagePath[1] = graphics/red_apple

另外,我的游戏是 2D 的,所以我希望学习曲线不要太陡,尽管我知道会有很多东西我可能从未遇到过并且不理解,但我认为这是最好的学习方式- 一点点天真也不会造成伤害:P

I am in the process of creating my first ever game for the mobile device and using Corona to do it in the Lua scripting language.

I've heard when creating each level inside a different file it is more memory-smart to put everything that stays the same into one logic file and load it inside each level file. What type of things should be inside this file, functions, sprites etc?

Also, how do I load this in the different files if all my variables are local?
I have tried require "logic" - my file name containing all my functions, however I'm not sure how I get the functions written inside to 'activate' because currently they don't.

If you need me to post any code I'm happy to do so.
Thanks for reading!


Update

Thanks for giving the idea of tables, I've stored all my functions inside one now, it looks like this:

-- Logic File 
local functionTable = {}
functionTable[1] = onTouch,
functionTable[2] = physicsStart,
functionTable[3] = onComplete,
functionTable[4] = winCondition

However I'm struggling to find a way to call these functions in my level files, here is what I have tried which isn't working, do you know of a way to improve this?

-- Level File
local logic = require "logic"
logic.functionTable[1]
logic.functionTable[2]
logic.functionTable[3]
logic.functionTable[4]

When you say store image names or paths, say I have an image called red_apple located in my 'graphics' folder in the root of my project; would the following be correct?

local imagePath = {}
imagePath[1] = graphics/red_apple

Also, my game is 2D so I'm hoping the learning curve isn't too steep although I understand there will be a lot of things I may have never come across and don't understand however I think that is the best way to learn - and a little bit of naivety never hurt as well :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2024-12-19 01:21:45

首先 - 如果这是您的第一款游戏,请从小处开始。如果你尝试从一切(关卡等)开始,你就会被复杂性所淹没。

现在,关于你的问题 - 这取决于。大多数时候,“关卡”只是普通的 lua 文件。

你可以让他们归还一张桌子。在该表中,您可以添加任何类型的 Lua 对象 - 字符串、函数、其他表等。

例如,在此 .lua 文件中,我返回一个包含两个字段的表:“难度”(整数)和“地图” (多行字符串)。这些可用于在“关卡加载器”功能上生成关卡。

-- level1.lua
local level = {}
level.difficulty = 1
level.map = [[
xxxxxx
x    x
x    x
xxxxxx  
]]
return level

关于图像 - 我认为你不能将它们直接存储在关卡中。但是您可以(例如)存储图像名称或路径。并让“地图加载器”在读取地图时“即时”加载这些新图像。

但正如我之前所说,这有点太复杂了。从较小的、没有级别的东西开始。

First - if it's your first ever game, start small. If you try to start with everything (levels, etc) you will get overrun by complexity.

Now, regarding your question - it depends. Most of the time, "levels" are just regular lua files.

You can make them return a table. Inside that table you can add any kind of Lua object - strings, functions, other tables, etc.

For example, on this .lua file I'm returning a table with two fields: 'difficulty' (an integer) and 'map' (a multiline string). Those could be used to generate the level on the "level loader" function.

-- level1.lua
local level = {}
level.difficulty = 1
level.map = [[
xxxxxx
x    x
x    x
xxxxxx  
]]
return level

Regarding images - I don't think you can store them directly in the level. But you could (for example) store image names or paths. And make the "map loader" load those new images "on the fly", while reading the map.

But as I said before, that is a bit too complex. Start with something smaller, without levels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文