《Cocos2d学习之路》三,学习使用TexturePacker创建动画效果

来源:互联网 发布:js定义数组 编辑:程序博客网 时间:2024/06/04 19:26

转载请说明出处:http://blog.csdn.net/lsmfeixiang/article/details/42214027

github地址:https://github.com/teffy/cocos2dx


这是一篇我个人在EOE发的blog《《Cocos2d学习之路》一,前序和闹钟》,由于eoe的blog系统实在是无言以对,我就把eoe上面的帖子以及blog是都转到csdn上来,原文地址:http://www.eoeandroid.com/blog-781265-51507.html


今天接着看教程,使用TexturePacker创建动画效果。

教程里的code是2.x的而且下载下来的资源缺少一些资源图,无奈在网上又搜索找了一个使用TexturePacker创建动画效果的blog,这个里面有齐全的图片资源,但是code是oc的貌似,可怜我这屌丝还是win8系统,mac没有,oc看不懂,就使用前面的code和后面blog的图片资源。看图:

一、下载的TexturePacker是3.6的版本,跟之前的版本界面不太一样,是先选平台,如图

后面的操作差不多,然后可以保存项目,public sprite sheet到项目的Resources下。
二、code的变化
1、3.x可以使用SpriteFrameCache SpriteFrame Animation,代替原来的以CC开头的,当然API的参数列表也是有变化的,需要具体查看定义
2、使用Vector<T>代替CCArray进行容器管理

贴一下Code:
Animation* AnimationCreator::createAnimWithFrameNameAndNum(const char* name, int num, float delay, unsigned int loops){
SpriteFrameCache* cache = SpriteFrameCache::sharedSpriteFrameCache();
Vector<SpriteFrame*> framesVector(num);
SpriteFrame* spriteFrame = NULL;
for (int i = 1; i < num; i++){
spriteFrame = cache->spriteFrameByName(String::createWithFormat("%s%d.png",name,i)->getCString());
if (spriteFrame == NULL){
break;
}
framesVector.pushBack(spriteFrame);
}
Animation* animation = Animation::createWithSpriteFrames(framesVector, delay, loops);
animation->setRestoreOriginalFrame(true);
return animation;
}

auto xiaopao = Sprite::create("res/xiaobing.png");
xiaopao->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(xiaopao);
SpriteFrameCache* cache = SpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("res/xiaobing.plist","res/xiaobing.png");
Animation* anim = AnimationCreator::createAnimWithFrameNameAndNum("xiaobing", 8, 0.1f, -1);
xiaopao->runAction(Animate::create(anim));

最后放一张效果图,其实是动的,不会做gif,有会的教教我,另外Android可运行


参考:
http://cn.cocos2d-x.org/tutorial/show?id=284
http://blog.csdn.net/u012945598/article/details/16988633
http://blog.csdn.net/time_anime/article/details/7754832

点击下载资源

0 0
原创粉丝点击