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

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

比方,14张帧图片,採用cocos2d-x lua的方法来写是这种,

 local sp = display.newSprite("grossini_dance_01.png", display.cx, display.cy)
self:addChild(sp) local animation = CCAnimation:create()
local number, name
for i = 1, 14 do
if i < 10 then
number = "0"..i
else
number = i
end
name = "grossini_dance_"..number..".png"
animation:addSpriteFrameWithFileName(name)
end animation:setDelayPerUnit(2.8 / 14.0) local action = CCAnimate:create(animation)
sp:runAction(action)

须要将其每一帧加入到CCAnimation中,和C++使用是一样的,可是quick的使用方法就是这样子的了,

 display.addSpriteFramesWithFile("hero.plist", "hero.png") --加入帧缓存

    local sp = display.newSprite("#grossini_dance_01.png", display.cx, display.cy)
self:addChild(sp)
local frames = display.newFrames("grossini_dance_%02d.png", 1, 14)
local animation = display.newAnimation(frames, 2.8/14.0)
sp:playAnimationOnce(animation)

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

  • string pattern 模式字符串
  • integer begin 起始索引
  • integer length 长度
  • boolean isReversed 是否是递减索引

此外注意的是,newFrames里面的图片名称一定是帧缓存里面的图片名称,所以换句话说,我们之前须要将图片们用图片打包工具处理下,假设是採用多个单张图片的形式,肯定是不行的,能够想到,我们后期图片肯定都是採用图片打包工具处理的,所以quick就直接封装了这种方法。

不信的话,能够看下这个函数的源码,

function display.newFrames(pattern, begin, length, isReversed)
local frames = {}
local step = 1
local last = begin + length - 1
if isReversed then
last, begin = begin, last
step = -1
end for index = begin, last, step do
local frameName = string.format(pattern, index)
local frame = sharedSpriteFrameCache:spriteFrameByName(frameName)
if not frame then
printError("display.newFrames() - invalid frame, name %s", tostring(frameName))
return
end frames[#frames + 1] = frame
end
return frames
end

直接是调用spriteFrameByName函数。

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

function Sprite:playAnimationOnce(animation, removeWhenFinished, onComplete, delay)
return transition.playAnimationOnce(self, animation, removeWhenFinished, onComplete, delay)
end function Sprite:playAnimationForever(animation, delay)
return transition.playAnimationForever(self, animation, delay)
end

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

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

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

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

在我用来,我认为像move,scale,fade这些单一的动作,我们用原生lua提供的那些就能够了,还easy被记住和使用,比方移动就使用CCMoveTo,还是挺好的。只是quick封装的个人认为非常不错的是,

transition.execute(target, action, args)

transition.sequence(actions)

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

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

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

  • delay: 等待多长时间后開始运行动作
  • easing: 缓动效果的名字及可选的附加參数,效果名字不区分大写和小写
  • onComplete: 动作运行完毕后要调用的函数
  • time: 运行动作须要的时间

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

  • backIn
  • backInOut
  • backOut
  • bounce
  • bounceIn
  • bounceInOut
  • bounceOut
  • elastic, 附加參数默觉得 0.3
  • elasticIn, 附加參数默觉得 0.3
  • elasticInOut, 附加參数默觉得 0.3
  • elasticOut, 附加參数默觉得 0.3
  • exponentialIn, 附加參数默觉得 1.0
  • exponentialInOut, 附加參数默觉得 1.0
  • exponentialOut, 附加參数默觉得 1.0
  • In, 附加參数默觉得 1.0
  • InOut, 附加參数默觉得 1.0
  • Out, 附加參数默觉得 1.0
  • rateaction, 附加參数默觉得 1.0
  • sineIn
  • sineInOut
  • sineOut

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

transition.execute(sprite, CCMoveTo:create(1.5, CCPoint(display.cx, display.cy)), {
delay = 1.0,
easing = "backout",
onComplete = function()
print("move completed")
end,
})

transition.sequence也是一个方便的函数,假设要是曾经,对于多个动作依次运行,咱们得这样,

 local move1  = CCMoveBy:create(1, ccp(250,0))
local move2 = CCMoveBy:create(1, ccp(0,50))
local array = CCArray:createWithCapacity(2)
array:addObject(move1)
array:addObject(move2)
local seq = CCSequence:create(array)

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

local sequence = transition.sequence({
CCMoveBy:create(1, ccp(250,0)),
CCMoveBy:create(1, ccp(0,50))
})

直接和C++的写法一样,依次创建加入进去就能够了,很方便~

以上就是所有内容了。

quick-cocos2d-x游戏开发【8】——动画与动作的更多相关文章

  1. iOS cocos2d 2游戏开发实战(第3版)书评

    2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...

  2. cocos2d-x游戏开发之动画

    MyGame.h中声明动画函数: class MyGame : public cocos2d::Layer{public: static Scene* createScene();    void U ...

  3. Android游戏开发研究帧动画实现

     1.动画的原则框架        帧的动画帧的动画顾名思义,画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在同样区域高速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,事实上只 ...

  4. (转载)如何学好iphone游戏开发

    转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...

  5. [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计

    声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...

  6. 【Cocos2D研究院之游戏开发】

    http://www.xuanyusong.com/archives/category/ios/cocos2d_game 分类目录归档:[Cocos2D研究院之游戏开发]   201211-19 Co ...

  7. 【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)

    1. Animation动画   在Android 中,系统提供了动画类 Animation ,其中又分为四种动画效果: ● AlphaAnimation:透明度渐变动画 ● ScaleAnimati ...

  8. 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9334121 作者:七十一雾央 新浪微博:http:/ ...

  9. 《MFC游戏开发》笔记五 定时器和简单动画

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...

  10. cocos2d 游戏开发实战

    文章转自:http://uliweb.clkg.org/tutorial/read/40 6   cocos2d 游戏开发实战 6.1   创建cocos2d项目 6.2   cocos2d v3 & ...

随机推荐

  1. MySQL—Install/Remove of the Service Denied

    在Windos7下通过命令"mysqld --install"安装MySQL数据库时出现了"Install/Remove of the Service Denied&qu ...

  2. WPF中实现验证码

    原文:WPF中实现验证码 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79563449 W ...

  3. C++利用SOAP开发WebService

    // soapconsole.cpp : Defines the entry point for the console application.// #include "stdafx.h& ...

  4. [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

    Every movie needs a director and every rented movie needs to exist in the store. How do we make sure ...

  5. 最小生成树(MST)求解旅行商问题

    从当前位置开始(也可以不指定起始位置),访问完所有未访问的端点后返回起始点的最短路径就是连接所有端点的生成树.最小生成树需保证: 每条边最多只能被选 1 次: 抹掉所有未被选择的边时,图形不能分为上下 ...

  6. [RxJS] Use RxJS concatMap to map and concat high order observables

    Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...

  7. Linux文件编辑命令具体整理

    刚接触Linux,前几天申请了个免费体验的阿里云server,选择的是Ubuntu系统.配置jdk环境变量的时候须要编辑文件. vi命令编辑文件,百度了一下,非常多回答不是非常全面,因此编辑文件话了一 ...

  8. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 《从零開始学Swift》学习笔记(Day 71)——Swift与C/C++混合编程之数据类型映射

    原创文章.欢迎转载.转载请注明:关东升的博客 posted @ 2017-07-21 13:23 zhchoutai 阅读(...) 评论(...) 编辑 收藏

  10. ie7easyui的书写要规范

    在书写easyui对象的属性,有时候习惯在,属性的末尾再加一个“,”,这个在高版本浏览器是没事的,但是在ie7及以下,会有报错