按寻路路径连续移动(方法2)相对完美

来源:互联网 发布:用php九九乘法表 编辑:程序博客网 时间:2024/06/05 15:56
function UnitNode:movePath (path, callBack)    self._spine:setAnimation(0, "run", true )------------------------------设置动画为run    local stepPath = {} -----------------创建一个路径table    for i = 1, path:len() do        stepPath[i] = cc.p(path:get(i)) -----------------------将路径放在路径table里面    end    local move    local function popStepAndAnimate()        if(# stepPath == 0) then            self._spine:setAnimation(0, "idle" , true)---------------------------设置动画为idle                if callBack then                    callBack()                end            return true        end        local pt = cc.p(stepPath[1]) -------------------------由于路径是按顺序存放的,所以最下面的总是当前step的的下一步               local selfX, selfY = self :getPosition()        local lenX = pt.x - selfX        local lenY = pt.y - selfY        local dist = math.sqrt (lenX * lenX + lenY * lenY)        if lenX > 0 then-----------------------------------------转向            self._spineBlur:setRotationSkewY(180)        else            self._spineBlur:setRotationSkewY(0)        end        self:stopAllActions()        if dist > 16 then--------------------------------------------------------对角和不对角的速度控制           move = cc.MoveTo:create((dist / 200), pt)        else           move = cc.MoveTo:create((dist * math.sqrt(2) / 200), pt)        end        local done = cc.CallFunc:create(popStepAndAnimate)---------------------------------------------创建当前函数为回调函数        table.remove(stepPath,1) ------------------------------------移除已经行走的step        local action = cc.Sequence:create(move, done, nil)------------------------------创建动作        self:runAction(action)----------------------------------------------开始动作    end    popStepAndAnimate()end

0 0
原创粉丝点击