quick-cocos2d-x游戏开发【8】——动画与动作
动画与动作,在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】——动画与动作的更多相关文章
- iOS cocos2d 2游戏开发实战(第3版)书评
2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...
- cocos2d-x游戏开发之动画
MyGame.h中声明动画函数: class MyGame : public cocos2d::Layer{public: static Scene* createScene(); void U ...
- Android游戏开发研究帧动画实现
1.动画的原则框架 帧的动画帧的动画顾名思义,画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在同样区域高速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,事实上只 ...
- (转载)如何学好iphone游戏开发
转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...
- [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计
声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...
- 【Cocos2D研究院之游戏开发】
http://www.xuanyusong.com/archives/category/ios/cocos2d_game 分类目录归档:[Cocos2D研究院之游戏开发] 201211-19 Co ...
- 【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)
1. Animation动画 在Android 中,系统提供了动画类 Animation ,其中又分为四种动画效果: ● AlphaAnimation:透明度渐变动画 ● ScaleAnimati ...
- 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9334121 作者:七十一雾央 新浪微博:http:/ ...
- 《MFC游戏开发》笔记五 定时器和简单动画
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...
- cocos2d 游戏开发实战
文章转自:http://uliweb.clkg.org/tutorial/read/40 6 cocos2d 游戏开发实战 6.1 创建cocos2d项目 6.2 cocos2d v3 & ...
随机推荐
- [RxJS] ReplaySubject with buffer
A BehaviorSubject can remember the latest value emitted, but what if we wanted Observer B to see all ...
- 页面中如何引用外部的HTML(四种方法)
页面中如何引用外部的HTML(四种方法) 一.总结 一句话总结:a.iframe标签 b.ajax引入代码片段 c.link import的方法导入 d.re ...
- Maven项目中mvn clean后找不到測试类问题
在Maven项目中进行单元測试,但mvn clean后又一次mvn install项目,再次进行单元測试.会有下面的错误. <span style="font-family:KaiTi ...
- 使用 Google Guava 美化你的 Java 代码:1~4 【转】
文章转载自:http://my.oschina.net/leejun2005/blog/172328 1.使用Google Collections,Guava,static imports编写漂亮代码 ...
- Size Balanced Tree(SBT树)整理
不想用treap和Splay,那就用SB树把,哈哈,其实它一点也SB,厉害着呢. 先膜拜一下作者陈启峰.Orz 以下内容由我搜集整理得来. 一.BST及其局限性 二叉查找树(Binary Search ...
- JVM调优2
原文地址:https://blog.csdn.net/sun1021873926/article/details/78002118 一.什么是JVM JVM是Java Virtual Machine ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- CreateFeature与CreateFeatureBuffer区别
转自原文CreateFeature与CreateFeatureBuffer区别 CreateFeature主要用于插入一条数据,CreateFeatureBuffer住哟啊用于插入多条数据,详细说明见 ...
- 《转》couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145,有须要的朋友能够參考下. 应为昨天安装的时候没及时 ...
- nyoj 949哈利波特(细节题)
哈利波特 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Harry 新学了三种魔法.他能够用第一种魔法把 a 克的沙子变成 b 克金属,能够用另外一种魔法把 c 克 ...