Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 如何导出,然后访问Lua中导出的方法_Oop_Lua - Fatal编程技术网

Oop 如何导出,然后访问Lua中导出的方法

Oop 如何导出,然后访问Lua中导出的方法,oop,lua,Oop,Lua,我有一个文件display.lua,其中有加载一些资源的代码 ----display.lua Resources = {} function Resources:new(rootdir) local newObj = {image = {}, audio = {}, root = ""} newObj.root = rootdir return setmetatable(newObj, self) end function Resources:getSpriteSheet(nam

我有一个文件display.lua,其中有加载一些资源的代码

----display.lua
Resources = {}

function Resources:new(rootdir)
  local newObj = {image = {}, audio = {}, root = ""}
  newObj.root = rootdir
  return setmetatable(newObj, self)
end

function Resources:getSpriteSheet(name)
    --- etc etc etc
end  
然后我有一个用于存储gamestate的游戏变量,它位于另一个文件game.lua中

---game.lua
require "display.lua"

function Game:new()
  local newObj = {mode = "", map = {}, player = {}, resources = {}}
  self.__index = self
  return setmetatable(newObj, self)
end

function Game:init()
  self.resources = Resources:new("/home/example/etc/game/")
  local spriteSheet = self.resources:getSpriteSheet("spritesheet.png")
end
我可以通过使用
require
访问资源代码。我的问题是,在
Game:init()
中,我无法访问
资源:getSpriteSheet()
,lua解释器抱怨“试图调用方法(getSpriteSheet)的值为零”


我在这里假设我必须导出参考资料中的方法,但我不知道该怎么做,因为我对Lua很陌生。

我想你想要
返回setmetatable(newObj,{uu index=self})
而不是
返回setmetatable(newObj,self)

另外,
require“display.lua”
可能应该是
require“display”
game.lua
应该在顶部的某个地方有
game={}
。有了这些变化,你的例子对我很有用