attempt to index local 'self' using MOAICoroutine in Lua
我只是从MOAI开始,我正在尝试使用MOAICoroutine创建传统的游戏循环。问题是,当我将使用30log构建的"类"的功能传递给它时,它返回错误。它似乎继续起作用,但是我想修复该错误。我的猜测是,MOAICoroutine正在使用点表示法而不是带有冒号的语法糖方法来调用函数。这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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函数内部的第二行(self.Ready做时)。
1
2
3
4 function GameSystem:Run()
if self:Init() then
self.Thread = MOAICoroutine.new ()
self.Thread:run(self.Start)My guess is that the MOAICoroutine is calling the function using the dot notation rather than the syntactical sugar method with a colon.
如何使用点符号(或冒号)来调用函数?时期或冒号的左侧是什么?您没有传递给它一个对象,只有一个函数。它的调用者将函数存储在表中的事实对此是完全未知的。它只是接收一个函数,然后调用它。
如果希望协程以方法调用开头,请在传递给coroutine.start的函数中执行此操作。
1 2 | self.Thread = MOAICoroutine.new () self.Thread:run(function() self:Start() end) |
重点是:
1 2 | function GameSystem:Start() end |
完全等同于:
1 2 | function GameSystem.Start(self) end |
完全等同于:
1 2 | GameSystem.Start = function(self) end |
等效于:
1 2 3 4 | function Foobar(self) end GameSystem.Start = Foobar |
如果我致电:
1 2 3 | print(Foobar) print(GameSystem.Start) print(someGameSystemInstance.Start) |