quick-cocos2d-x游戏开发【8】——动画与动作

前端之家收集整理的这篇文章主要介绍了quick-cocos2d-x游戏开发【8】——动画与动作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

动画与动作,在quick中都有对其封装,所以我们还是来看一下吧。

总的来说,对于帧动画,quick封装的方法我们可以经常使用,这是非常方便的,下面直接上代码来直观感受下,

比如,14张帧图片,采用cocos2d-x lua的方法来写是这样的,

  1. localsp=display.newSprite("grossini_dance_01.png",display.cx,display.cy)
  2. self:addChild(sp)
  3. localanimation=CCAnimation:create()
  4. localnumber,name
  5. fori=1,14do
  6. ifi<10then
  7. number="0"..i
  8. else
  9. number=i
  10. end
  11. name="grossini_dance_"..number..".png"
  12. animation:addSpriteFrameWithFileName(name)
  13. end
  14. animation:setDelayPerUnit(2.8/14.0)
  15. localaction=CCAnimate:create(animation)
  16. sp:runAction(action)

需要将其每一帧添加到CCAnimation中,和C++使用是一样的,但是quick的用法就是这样子的了,

[html] copy
    display.addSpriteFramesWithFile("hero.plist","hero.png")--添加帧缓存
  1. localsp=display.newSprite("#grossini_dance_01.png",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> localframes=display.newFrames("grossini_dance_%02d.png",1,14)
  2. localanimation=display.newAnimation(frames,2.8/14.0)
  3. sp:playAnimationOnce(animation)

display.newFrames(pattern,begin,length,isReversed)的各个参数的意义是,

  • stringpattern模式字符串
  • integerbegin起始索引
  • integerlength长度
  • booleanisReversed是否是递减索引
此外注意的是,newFrames里面的图片名称一定是帧缓存里面的图片名称,所以换句话说,我们之前需要将图片们用图片打包工具处理下,如果是采用多个单张图片的形式,肯定是不行的,可以想到,我们后期图片肯定都是采用图片打包工具处理的,所以quick就直接封装了这个方法

不信的话,可以看下这个函数的源代码

copy
    functiondisplay.newFrames(pattern,isReversed)
  1. localframes={}
  2. localstep=1
  3. locallast=begin+length-1
  4. ifisReversedthen
  5. last,begin=begin,last
  6. step=-1
  7. forindex=begin,last,stepdo
  8. localframeName=string.format(pattern,index)
  9. localframe=sharedSpriteFrameCache:spriteFrameByName(frameName)
  10. ifnotframethen
  11. printError("display.newFrames()-invalidframe,name%s",tostring(frameName))
  12. return
  13. frames[#frames+1]=frame
  14. returnframes
  15. end


直接是调用spriteFrameByName函数


对于播放动画,quick给Sprite精灵类提供了两个函数

copy
    functionSprite:playAnimationOnce(animation,removeWhenFinished,onComplete,delay)
  1. returntransition.playAnimationOnce(self,animation,delay)
  2. functionSprite:playAnimationForever(animation,delay)
  3. returntransition.playAnimationForever(self,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> end

一个是播放动画一次,一个是永久播放动画。好用!


以上就是动画的用法,接下来我们再看关于动作的使用,

动作封装的类是transition,其中提供了这些函数

@H_544_403@ @H_280_404@transition.newEasing(action,easingName,more) @H_544_403@ @H_544_403@ @H_280_404@transition.execute(target,action,args) @H_544_403@ @H_544_403@ @H_280_404@transition.rotateTo(target,args) @H_544_403@ @H_544_403@ @H_280_404@transition.moveTo(target,args) @H_544_403@ @H_544_403@ @H_280_404@transition.fadeTo(target,args) @H_544_403@ @H_544_403@ @H_280_404@transition.scaleTo(target,args) @H_544_403@ @H_544_403@ @H_280_404@transition.sequence(actions) @H_544_403@ @H_544_403@ @H_280_404@transition.playAnimationOnce(target,delay) @H_544_403@
为图像创造效果
执行一个动作效果
显示对象旋转到指定角度,并返回 CCAction 动作对象。
显示对象移动到指定位置,并返回 CCAction 动作对象。
显示对象的透明度改变为指定值,并返回 CCAction 动作对象。
显示对象缩放到指定比例,并返回 CCAction 动作对象。
创建一个动作序列对象。
显示对象上播放一次动画,并返回 CCAction 动作对象。

在我用来,我觉得像move,scale,fade这些单一的动作,我们用原生lua提供的那些就可以了,还容易被记住和使用,比如移动就使用CCMoveTo,还是挺好的。不过quick封装的个人觉得很不错的是,

transition.execute(target,args)

transition.sequence(actions)

这两个,为啥呢,接着看,


transition.execute() 是一个强大的工具,可以为原本单一的动作添加各种附加特性。

transition.execute() 的参数表格支持下列参数:

transition.execute() 支持的缓动效果

这个函数可以完成运动中的速度效果,以及CCCallFunc,CCDelayTime等功能,将其附加在一起,就不用写繁琐的函数嵌套和CCSequence了。廖大真是写到我的心坎里去了。像这样,

copy
    transition.execute(sprite,CCMoveTo:create(1.5,CCPoint(display.cx,display.cy)),{
  1. delay=1.0,
  2. easing="backout",
  3. onComplete=function()
  4. print("movecompleted")
  5. end,
  6. })

transition.sequence也是一个方便的函数,如果要是以前,对于多个动作依次执行,咱们得这样,

copy
    localmove1=CCMoveBy:create(1,ccp(250,0))
  1. localmove2=CCMoveBy:create(1,ccp(0,50))
  2. localarray=CCArray:createWithCapacity(2)
  3. array:addObject(move1)
  4. array:addObject(move2)
  5. localseq=CCSequence:create(array)

要把每个动作装在数组里面,然后才能创建一个CCSequence,而现在呢,

copy
    localsequence=transition.sequence({
  1. CCMoveBy:create(1,0)),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCMoveBy:create(1,50))
  2. })

直接和C++的写法一样,依次创建添加进去就可以了,非常方便~


以上就是全部内容了。

原文链接:https://www.f2er.com/cocos2dx/343328.html

猜你在找的Cocos2d-x相关文章