cocos动画
顺序动画和融合动画
融合动画:cc.Spawn:create()
顺序动画:cc.Sequence:create()
local bazier=cc.MoveTo
local sequence=cc.Sequence:create(cc.Spawn:create(bazier,rotate),cc.CallFunc:create(callFunc),nil)
动画
1.贝塞尔曲线
--获得一个贝塞尔曲线的Action
--@param posFrom 运动起点
--@param posTo 运动结束点
--@param time 运动的持续时间
--@return Action
function GameArea:bezier(posFrom,posTo,duration)
local posMid = self:getMidPoint(posFrom,posTo,duration)
local points = {posFrom,posMid,posTo}
local action = cc.BezierTo:create(duration,points)
return action
end
--此方法是将y轴抬高,返回两个点的中间坐标
--@param posFrom 运动起点
--@param posTo 运动结束点
--@param time 运动的持续时间
--@return 返回贝塞尔曲线的中间坐标
function GameArea:getMidPoint(posFrom,posTo,time)
local y = posFrom.y
if posTo.y > y then
y = posTo.y
end
local y = y * 3 /2
local posRet = cc.p((posFrom.x + posTo.x) / 2,y)
return posRet
end
--获得整个进行贝塞尔曲线的时间,时间的计算方式是两点的距离*速度
--@param posFrom 运动起点
--@param posTo 运动结束点
--@return 返回float类型的时间
function GameArea:getDropTime(posFrom,posTo,speed)
local time = 10
local distance = (posFrom.x - posTo.x) * (posFrom.x - posTo.x) + (posFrom.y - posTo.y) * (posFrom.y - posTo.y)
if distance < 0 then
distance = -distance
end
distance = math.sqrt(distance)
time = distance*speed
return time
end
2.旋转
local rotate= cc.RotateTo:create(duration, 30)
移动
local move= cc.MoveTo:create(duration,cc.p(0,9))
EaseSineInOut
local girlAction= cc.EaseSineInOut:create(cc.MoveTo:create(2,cc.p(imageGirl:getPositionX(),imageGirl:getPositionY()+4)))
local girlBackAction = cc.EaseSineInOut:create(cc.MoveTo:create(2,cc.p(imageGirl:getPositionX(),imageGirl:getPositionY()-4)))
local temSeq = cc.Sequence:create(girlAction,girlBackAction)
imageGirl:runAction(cc.RepeatForever:create(temSeq))
帧动画
根据图片生成帧动画
CharmAction.charmActIndex = {
[10301] = 7,
[10302] = 5,
[10309] = 8,
[10303] = 5,
[10307] = 7,
[10310] = 8
}
resConfig.ACT_CHARM_N = "GameCommon/GameCharm/imge/charm_act_%d_%d.png"
function CharmAction:getCharmAct(index)
local animation = cc.Animation:create()
for i = 0,self.charmActIndex[index]-1 do
local szName = string.format(resConfig.ACT_CHARM_N,index,i)
animation:addSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame(szName))
end
animation:setDelayPerUnit(3.0 / 14)
animation:setRestoreOriginalFrame(true)
local action = cc.Animate:create(animation)
return action
end
根据csb对象播放动画
function play2DAnimation(position)
local animationNode=cc.CSLoader:createNodeWithVisibleSize("")
local animation = cc.CSLoader:createTimeline("")
if animation and animationNode then
animationNode:runAction(animation)
animation:gotoFrameAndPlay(0,false)
end
end
--设置回调
-- 播放2D闪电动画
-- @parent 父节点
-- @position 位置坐标 屏幕坐标
function HuDanFangMah:play2DAnimation(parent,position)
local animationNode=cc.CSLoader:createNodeWithVisibleSize(m3D.MahSourcePath.huAnimationNodePath)
if animationNode then
local animation = cc.CSLoader:createTimeline(m3D.MahSourcePath.huAnimationNodePath)
--动画1 播放闪电动画
local animationFunc=cc.CallFunc:create(function ()
animation:gotoFrameAndPlay(0,false)
animationNode:runAction(animation)
end)
--动画2 销毁闪电节点
local callFunc=cc.CallFunc:create(function ()
animationNode:removeFromParent()
animationNode:setVisible(false)
end)
animation:setLastFrameCallFunc(callFunc)
--播放动画
animationNode:runAction(animation)
parent:addChild(animationNode)
animationNode:setPosition(position)
end
end
--如果存在着在动画中间需要其他的回调。则只能通过切割动画,回调里面套动画
function GameScene:showGameStartAni(callBackFunc)
local gameStartAni = cc.CSLoader:createNode(resConfig.gameStartAni)
local ani = cc.CSLoader:createTimeline(resConfig.gameStartAni)
gameStartAni:stopAllActions()
self._mah3DUILayer:addChild(gameStartAni)
--回调,开始另一个动画
local continueAnimation=function (...)
ani:gotoFrameAndPlay(65,80,false)
local particle=wolf.UIManager.seekNodeByName(gameStartAni,"Particle_1")
particle:setVisible(true)
local callfun = function (...)
gameStartAni:removeFromParent(true)
if callBackFunc ~= nil then
callBackFunc()
end
end
ani:setLastFrameCallFunc(callfun)
end
gameStartAni:runAction(ani)
ani:gotoFrameAndPlay(0,65, false)
ani:setLastFrameCallFunc(continueAnimation)
end
//在C++中还有另一个方法是直接添加帧回调,以下代码没有验证过
void animationCallFunc(Frame *frame){
if(frame==nullptr)return;
if(frame->getEvent()=="END"){
}
}
void addTimeLineCallFunc(ActionTimeline timeLine){
timeLine:setFrameEventCallFunc(animationCallFunc); //这个还需要在cocos studio中添加帧事件
}
//在lua中不支持这个方法,lua_cocos2dx_coco_studio_manual.cpp中lua-binding函数直接返回
int lua_cocos2dx_studio_ActionTimeline_removeTimeline(lua_State* tolua_S)
{
//.....................
if (argc == 1) //当参数的数量为1的时候直接跳出函数
{
cocostudio::timeline::Timeline* arg0;
ok &= luaval_to_object<cocostudio::timeline::Timeline>(tolua_S, 2, "ccs.Timeline",&arg0, "ccs.ActionTimeline:removeTimeline");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_studio_ActionTimeline_removeTimeline'", nullptr);
return 0;
}
cobj->removeTimeline(arg0);
lua_settop(tolua_S, 1);
return 1;
}
//............
return 0;
}