【问题标题】:What can i do to address a parent in lua?我能做些什么来解决lua中的父母?
【发布时间】:2020-09-26 15:48:12
【问题描述】:

我正在制作一款游戏,玩家需要在其中生成一些子弹:

game
├──player = {}
└──bullets = {}

我在 godot 中听说你可以使用 parent 来寻址父节点,想知道我是否可以以某种方式模仿。

我目前有 2 个可能的解决方案:

local game = {
    player = player.new(x, y, spawnBullet),
    bullets = {}
}

spawnBullet 是一个函数。虽然这需要对所有功能都这样做,这可能很烦人。

我的第二个解决方案是将游戏项目传递给玩家

local game
game = {
    player = player.new(x, y, game),
    bullets = {},

player.new(x, y, parent)

player.parent = parent

这些解决方案是全部可用还是有更好的解决方案?

不管怎样,我自己做了一个节点模块。

local node = {}
node.__index = node

local new = function(name, parent)
    assert(name, "Please provide a name for the node")
    local newNode = {
        name = name,
        parent = parent,
    }
    return setmetatable(newNode, node)
end

return setmetatable(
    {
        new = new,
        __call = new,
    },
    node
)

【问题讨论】:

    标签: lua love2d class-hierarchy


    【解决方案1】:

    您使用的是引擎/框架吗?

    我正在使用 gideros,我是这样做的,例如:

    主游戏

    -- 我的播放器

    -- 子弹

    在我的播放器类中:

    -- SHOOT
    if self.isspace then
        local bullet = Character_Bullet.new(self.world, ...)
        self:getParent():addChild(bullet)
        self.isspace = false
    end
    

    【讨论】:

    • 我正在使用love2d,我在询问如何模拟getParent() 的行为与表层次结构以及是否有可能
    • 好吧,我已经为 love2d 寻找 getparent 了,但这并不简单。您可以为播放器添加项目符号表。像 player.bullet = bullet 之类的东西。
    猜你喜欢
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多