基本上:
- 我正在 LÖVE 引擎中制作游戏,您可以在其中单击以创建块
- 每次单击时,鼠标 X 和鼠标 Y 上都会创建一个块
- 但是,我只能让一个块出现,因为我必须将该块(或表)命名为“object1”
有什么方法可以创建一个又一个表并增加值?(如 object1{}、object2{}、object3{} 等……但在主表中,'created_objects')
但只有当点击时,我想排除循环部分(但如果不是请告诉我)
到目前为止,这是我的代码,但无法编译。
function object_create(x, y, id) **--Agruments telling the function where the box should spawn and what the ID of the box is (variable 'obj_count' which increases every time a box is spawned)**
currobj = "obj" .. id **--Gives my currently created object a name**
crob.[currobj] = {} **--Is supposed to create a table for the current object, which holds all of its properties. **
crob.[currobj].body = love.physics.newBody(world, x, y, "dynamic")
crob.[currobj].shape = love.physics.newRectangleShape(30, 30)
crob.[currobj].fixture = love.physics.newFixture(crob.[currobj].body, crob.[currobj].shape, 1) **--The properties**
crob.[currobj].fixture:setRestitution(0.3)
但是我应该用什么替换 [currobj]?
解决了
找到了我要找的东西。如果人们想知道,这是代码:
function block_create(x, y, id) --(鼠标x和y,以及增加的变量)
blocks[id] = {}
blocks[id][1] = love.physics.newBody(world, x, y, "dynamic")
blocks[id][2] = love.physics.newRectangleShape(45, 45)
blocks[id][3] = love.physics.newFixture(blocks[id][1], blocks[id][2])
blocks[id][3]:setRestitution(0.2)
blocks[id][4] = math.random(0, 255) --The Color
blocks[id][5] = math.random(0, 255)
blocks[id][6] = math.random(0, 255)
blockcount = blockcount + 1