定义平台表的更短方法

时间:2016-08-21 19:08:04

标签: lua lua-table love2d

所以基本上我有这个代码,控制我的平台(我想为游戏创建一个2d平台游戏)(Love2D Lua)这里的剧本

platforms = {}
platform1 = { x = 0, y = 600, width = 279, height = 49 }
platform2 = { x = 279, y = 600, width = 279, height = 49 }
platform3 = { x = 558, y = 600, width = 279, height = 49 }
table.insert(platforms, platform1)
table.insert(platforms, platform2)
table.insert(platforms, platform3) 

有没有更好的方法来控制/创建平台?

4 个答案:

答案 0 :(得分:2)

如果您的平台有其共同的大小,您可以使用以下内容:

platforms = {
    { x = 0, y = 600 },
    { x = 279, y = 600 },
    { x = 558, y = 600 },
};
for _,v in ipairs(platforms) do
    v.width = 279;
    v.height = 49;
end

答案 1 :(得分:1)

使用"scripts": { "test": "mocha --grep ./e2e.js --invert --compilers js:babel-register -R spec --ui bdd --timeout 7000" } 作为数组(看起来像你想要的那样):

platforms

答案 2 :(得分:0)

由于只有x值发生了变化:

platforms = {}
for _, x in ipairs{0, 279, 558} do
    table.insert(platforms, {x = x, y = 600, width = 279, height = 49})
end

答案 3 :(得分:0)

正如其他人所说,要声明它们,你可以在声明时将它们放在表格中。

platforms = {
    { x = 999, y = 999, w = 999, h = 99 },
    ...
}

一旦你建立了这个表,你就可以随时添加它。

table.insert(platforms, { x = 111, y = 111, w = 111, h = 111 }

或者带走一个,让你知道它是哪个索引(它在列表的下方多远)

table = {
    { }, -- Index 1
    { }, -- Index 2
    ...
}

然后带走一个:

table.remove(platforms, 1)  

这将删除索引1下的平台。

要筛选它们,你可以使用ipairs。

for i, v in ipairs(platforms) do
    v.y = v.y - 10 * dt -- Do something, this would slowly raise all platforms
end

这应该就是你所需要的,玩得开心!

相关问题