Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
Gideros如何将函数传递给Timer.delayCall?_Timer_Lua_Gideros - Fatal编程技术网

Gideros如何将函数传递给Timer.delayCall?

Gideros如何将函数传递给Timer.delayCall?,timer,lua,gideros,Timer,Lua,Gideros,首先,可以这样做: local delay = 1000 local timer = Timer.delayedCall(delay, function(data) print(data) end, 20) function G

首先,可以这样做:

local delay = 1000
local timer = Timer.delayedCall(delay, 
                                function(data) 
                                    print(data)
                                end, 
                                20)
function Game:DoSomething(data)
   print(data)
end

local timer = Timer.delayedCall(delay, 
                                self.DoSomething, 
                                20) 
function Game:DoSomething(data)
   print(data)
end

function invokeMethod(args)
    local func=table.remove(args,1)
    func(unpack(args))
end

local timer = Timer.delayedCall(
      delay,
      invokeMethod,
      {self.DoSomething,self,20}) --self is the instance you are calling
但是,这样做似乎是不可能的:

local delay = 1000
local timer = Timer.delayedCall(delay, 
                                function(data) 
                                    print(data)
                                end, 
                                20)
function Game:DoSomething(data)
   print(data)
end

local timer = Timer.delayedCall(delay, 
                                self.DoSomething, 
                                20) 
function Game:DoSomething(data)
   print(data)
end

function invokeMethod(args)
    local func=table.remove(args,1)
    func(unpack(args))
end

local timer = Timer.delayedCall(
      delay,
      invokeMethod,
      {self.DoSomething,self,20}) --self is the instance you are calling

换句话说,我想在外部定义函数,以便其他人重用。然而,这似乎是不可能的。还是我做错了?

如果您想要的是使用Timer.delayedCall以通用方式调用具有多个参数的方法,您可以这样做:

local delay = 1000
local timer = Timer.delayedCall(delay, 
                                function(data) 
                                    print(data)
                                end, 
                                20)
function Game:DoSomething(data)
   print(data)
end

local timer = Timer.delayedCall(delay, 
                                self.DoSomething, 
                                20) 
function Game:DoSomething(data)
   print(data)
end

function invokeMethod(args)
    local func=table.remove(args,1)
    func(unpack(args))
end

local timer = Timer.delayedCall(
      delay,
      invokeMethod,
      {self.DoSomething,self,20}) --self is the instance you are calling

PS:这是我在上的第一篇博文,如果格式不正确,很抱歉…

不要定义为游戏:DoSomething。只是做点什么。而且,不需要自作主张。self在方法body.oO之外不可用。现在我觉得自己很愚蠢。我认为理性定义为游戏:DoSomething是在一个精灵类中。但是没有必要这样做。。。谢谢你。我能再多问一点吗?现在,如果我只是DoSomething,它就可以工作了,但是如何访问DoSomething中的“self”属性呢?如果没有方法定义,就没有self。请阅读:和