2
此代码在Lua将函数应用到值高阶“办法”
function Apply(f, value)
return f(value)
end
然后我就可以这样使用它适用任意函数调用我的游戏对象,像这样
Apply(Draw, GameObject)
Apply(Update, GameObject)
是否有可能,而不是做什么,我会,可能不正确,请致电高阶方法
function GameObject:Apply(f)
return self:f()
end
我最终婉要做的是有一个GameObjects表,我可以批量调用Methods。因此,使用这种“更高阶方法”的概念,甚至可能不存在,我会创建代码,执行以下操作。
...
--Create the batch object with three bullets in it
BatchGameObjects = BatchGameObject:new(Bullet1, Bullet2, Bullet3)
--Call equivelent to
--Bullet1:DrawMethod()
--Bullet2:DrawMethod()
--Bullet3:DrawMethod()
--Bullet1:UpdateMethod()
--Bullet2:UpdateMethod()
--Bullet3:UpdateMethod()
BatchGameObjects:Apply(DrawMethod)
BatchGameObjects:Apply(UpdateMethod)
谢谢,这工作很好。其他人使用它的一个潜在问题是,当你使用你想要将它作为字符串传递给它的函数时。 IE BatchGameObjects:应用(“绘制”)。谢谢!!! – user514156 2014-09-28 23:40:39
@ user514156,是的,这就是我的意思,当我移情“名称”。 – 2014-09-29 11:10:02