BattleUnit = {} function BattleUnit.New(go, role, position, root, isBoss) local o = {} setmetatable(o, {__index = BattleUnit}) return o end function BattleUnit:ctor(go, role, position, root, isBoss,enemyId) self._DelayFuncList = {} self._LoopFuncList = {} if self.onCreate then self:onCreate(go, role, position, root, isBoss,enemyId) end end -- function BattleUnit:OnSortingOrderChange(battleSorting) return end function BattleUnit:Dispose() -- 清空所有延迟方法 self:ClearDelayFunc() self:ClearLoopFunc() if self.onDispose then self:onDispose() end end function BattleUnit:onDispose() end --++++++++++++++DelayFunc 延迟执行方法 function BattleUnit:DelayFunc(time, func) -- 判断是否需要延时 if not time or time <= 0 then if func then func() end return end -- if not self._DelayFuncList then self._DelayFuncList = {} end local timer timer = Timer.New(function () if self._DelayFuncList[timer] then self._DelayFuncList[timer]() end self:ClearDelayFunc(timer) end, time) timer:Start() -- self._DelayFuncList[timer] = func end function BattleUnit:ClearDelayFunc(t) if not self._DelayFuncList then return end if t then if self._DelayFuncList[t] then t:Stop() self._DelayFuncList[t] = nil end else for timer, func in pairs(self._DelayFuncList) do timer:Stop() end self._DelayFuncList = {} end end --++++++++++++++DelayFunc --++++++++++++++LoopFunc 循环执行方法 function BattleUnit:LoopFunc(time, count, func) if count <= 0 then LogError("无法用于无限循环的方法") return end if not self._LoopFuncList then self._LoopFuncList = {} end local timer = nil local ctr = 0 timer = Timer.New(function () if func then func() end ctr = ctr + 1 if ctr >= count then self:ClearDelayFunc(timer) end end, time, count) timer:Start() table.insert(self._LoopFuncList, timer) end function BattleUnit:ClearLoopFunc(t) if not self._LoopFuncList then return end if t then local rIndex for index, timer in ipairs(self._LoopFuncList) do if timer == t then rIndex = index break end end if rIndex then self._LoopFuncList[rIndex]:Stop() table.remove(self._LoopFuncList, rIndex) end else for _, timer in ipairs(self._LoopFuncList) do timer:Stop() end self._LoopFuncList = {} end end --++++++++++++++LoopFunc