尝试在Lua中使用MOAICoroutine索引本地变量'self'。
2013-1-29 15:56:6
收藏:0
阅读:129
评论:1
我刚开始学习MOAI,正在尝试使用MOAICoroutine创建一个传统的游戏循环。问题是当我将函数传递给使用30log构建的“class”时,它会返回一个错误。它似乎仍然工作,但我想修复这个错误。我的猜测是,MOAICoroutine正在使用点表示法调用该函数,而不是带有冒号的语法糖方法。这是代码:
class = require “30log.30log”
GameSystem = class ()
function GameSystem:__init(Name, Title)
self.Name = Name
self.Title = Title
self.Ready = false
end
function GameSystem:Run()
if self:Init() then
self.Thread = MOAICoroutine.new ()
self.Thread:run(self.Start)
--self:Start()
return true
else
print(“Init failed.”)
return false
end
end
function GameSystem:Init()
print(“Initializing Game System”)
if not self:InitTimer() then return false end
if not self:InitWindow(640,480) then return false end
if not self:InitViewport() then return false end
if not self:InitGraphics() then return false end
if not self:InitSound() then return false end
if not self:InitInput() then return false end
self.Ready = true
return true
end
function GameSystem:Start()
print(“Starting Game System”)
while self.Ready do
self:UpdateTimer()
self:UpdateGraphics()
self:UpdateSound()
self:UpdateInput()
coroutine.yield()
end
end
function GameSystem:InitTimer()
return true
end
function GameSystem:InitWindow(width, height)
print(“Initializing Window”)
return true
end
function GameSystem:InitViewport()
print(“Initializing Viewport”)
return true
end
function GameSystem:InitGraphics()
print(“Initializing Graphics”)
return true
end
function GameSystem:InitSound()
print(“Initializing Sound”)
return true
end
function GameSystem:InitInput()
print(“Initializing Input”)
return true
end
function GameSystem:UpdateTimer()
--print(“Updating Timer”)
return true
end
function GameSystem:UpdateGraphics()
--print(“Updating Graphics”)
return true
end
function GameSystem:UpdateSound()
--print(“Updating Sound”)
return true
end
function GameSystem:UpdateInput()
--print(“Updating Input”)
return true
end
是30log类代码引起了这个问题吗?我尝试过各种方法。我很确定它尝试访问的self是第一个参数,即mytable.myfunction(self, myarg)。有什么想法可以修复这个nil值引用的错误。实际上,错误出现在Start函数中的第二行(while self.Ready do)。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
function GameSystem:Run() 如果 self:Init() 返回 true,则 self.Thread = MOAICoroutine.new () self.Thread:run(self.Start) end我猜测 MOAICoroutine 调用该函数时使用的是点表示法,而不是冒号语法糖方法。
它将点表示法 (或冒号表示法) 用于调用函数吗?在点或冒号的左边会有什么?您没有传递对象,只传递了函数。它完全不知道调用者正在将该函数存储在表中。它只接收一个函数并调用它。
如果您想让协程以方法调用的方式启动,请在传递给
coroutine.start的函数中执行该操作:self.Thread = MOAICoroutine.new () self.Thread:run(function() self:Start() end)关键点是:
function GameSystem:Start() end与以下三种形式完全等价:
function GameSystem.Start(self) endGameSystem.Start = function(self) endfunction Foobar(self) end GameSystem.Start = Foobar如果我调用:
print(Foobar) print(GameSystem.Start) print(someGameSystemInstance.Start)print将收到相同的值。在 Lua 中,函数就是函数,不会因被存储在 table 中而被 "感染",以便第三方拥有对该函数的引用时可以知道您希望它作为某个特定 'class instance' 的方法调用。