在Love2d和Lua中的继承

我有一个包含这些值和函数的类:

require("class")

entity = class:new()

function entity:new()
    self.x = 100
    self.y = 100
    self.width = 32
    self.height = 32
    self.info = "entity"
    self.alive = true
    self.color = {r = 255, g = 0, b = 0}

    return self
end

function entity:load()
end

function entity:update()
    if self.alive then
    end
end

function entity:draw()
    if self.alive then
        love.graphics.setColor(self.color.r, self.color.g, self.color.b)
    end
end

function entity:destroy()
    self.alive = false
end

我想要能够简单地将这些相同的函数和值用于其他类,例如:

require("entity")

local player = entity:new()

function player:load()
   self.color.r = 100
end

function player:update()
end
--等等

我来自 flash 和 As3 背景,如果你们中有人了解的话,你们可能会更多或更少地理解我正在尝试做什么。 所以有谁能帮帮我吗?所有的帮助都会受到赞赏。

点赞
用户1955931
用户1955931

你可以使用这个class system 来获得你想要的经验。(请确保复制完整版的代码)。

你的代码看起来像这样:

require("class")

entity= class()

entity.x = 100
entity.y = 100
entity.width = 32
entity.height = 32
entity.info = "entity"
entity.alive = true
entity.color = {r = 255, g = 0, b = 0}

function entity:load()
end

function entity:update()
    if self.alive then
        -- entity 更新内容
    end
end

function entity:draw()
    if self.alive then
        love.graphics.setColor(self.color.r, self.color.g, self.color.b)
    end
end

function entity:destroy()
    self.alive = false
end

第二个文件:

require("entity")

local player = class()
player:addparent(entity)

function player:load()
    self.color.r = 100
end

function player:update()
end
2014-04-24 18:14:19