前言 : 战斗里面需要做到兵种,展现攻击动作,发射子弹,子弹爆炸,显示伤害,然后报告本次攻击已经完成。
解决方法有: 1, 用callBack回调,每步回调callback回来,即发射子弹->回调显示伤害->回调战斗完成。太繁琐了 2, 用lua协程,将异步以同步的方法写出来。清晰明了
核心代码如下 :
local function attack() local arr = { cc.DelayTime:create(0.25), cc.CallFunc:create(function() coroutine.resume(self.coroutine) end), } self.sPRite3D:runAction(cc.Sequence:create(arr)) return coroutine.yield() end local function bullet() for i = 1, #targets do local target = targets[i] local arr = { cc.DelayTime:create(0.25), cc.CallFunc:create(function() coroutine.resume(self.coroutine) end), } self.sprite3D:runAction(cc.Sequence:create(arr)) coroutine.yield() end end local function reportFnish() print("end") end self.coroutine = coroutine.create(function () attack() bullet() reportFinish() end) coroutine.resume(self.coroutine)新闻热点
疑难解答