如何从Love2D中的子类调用超类中的覆盖方法?
Sprite = {}
function Sprite:new(x, y, image)
local self = {}
self.x = x
self.y = y
self.image = image
function self:draw()
love.graphics.draw(self.image, self.x, self.y)
end
return self
end
ExplorerWindow = {}
function ExplorerWindow:new(x, y, image)
local self = Sprite:new(x, y, image)
function self:draw()
-- Here I want to call draw in Sprite
end
return self
end
function love.load()
explorerWindow = ExplorerWindow:new(200, 200, love.graphics.newImage("res/window.png"))
end
function love.draw()
explorerWindow:draw()
end
答案 0 :(得分:1)
将旧函数存储在临时变量中。
function ExplorerWindow:new(x, y, image)
local self = Sprite:new(x, y, image)
local oldDraw = self.draw
function self:draw()
oldDraw(self)
end
return self
end
另外,请相应地使用setmetatable
。