【发布时间】:2014-06-10 02:44:04
【问题描述】:
我有一个具有这组值和函数的类:
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
--etc etc
我来自 Flash 和 As3 背景,如果你们中的任何人知道,你可能或多或少地理解我想要做什么。 那么有人可以帮我解决这个问题吗?感谢所有帮助。
【问题讨论】:
-
你用的是什么类库? Lua 中没有内置类。您确定
entity:new()创建的是子类而不是实体实例(如果有区别)?
标签: inheritance lua love2d