如何编辑网站模板,在线制作头像生成,xp系统做网站服务器,做网站用的各种图标大全C#(wp7)兄弟篇Learning Cocos2d-x for XNA#xff08;7#xff09;——让Sprite动起来 本讲将详细介绍Cocos2d-x游戏中动画Animate的创建方式#xff0c;通过逐帧数组播放动画和创建动画集合播放动画#xff0c;比较两者的异同#xff0c;让Sprite动起来。 工程文件#… C#(wp7)兄弟篇Learning Cocos2d-x for XNA7——让Sprite动起来 本讲将详细介绍Cocos2d-x游戏中动画Animate的创建方式通过逐帧数组播放动画和创建动画集合播放动画比较两者的异同让Sprite动起来。 工程文件SpriteAnimationTest.h和SpriteAnimationTest.cpp SpriteAnimationTest.h 添加两类预处理SpriteAnimateFrameScene和SpriteAnimateFrameLayer 核心代码 SpriteAnimationTest.h 1 #ifndef _SPRITE_ANIMATION_TEST_2 #define _SPRITE_ANIMATION_TEST_3 4 #include cocos2d.h5 6 using namespace cocos2d;7 8 class SpriteAnimateFrameScene:public CCScene9 {
10 public:
11 SpriteAnimateFrameScene();
12 ~SpriteAnimateFrameScene();
13
14 virtual void onEnter();
15 };
16
17 class SpriteAnimateFrameLayer:public CCLayer
18 {
19 public:
20 SpriteAnimateFrameLayer();
21 ~SpriteAnimateFrameLayer();
22 };
23
24 #endif //_SPRITE_ANIMATION_TEST_ 通过逐帧数组播放动画CCSpriteFrame的使用 动画是通过逐帧连续播放图像而形成的动作画面。 既然是逐帧动画细化话后即是单帧通过记录单帧信息然后再将单帧连续起来即是逐帧动画。 添加素材文件到Assets/Sprite中named为PlayerRun.png 在SpriteAnimateFrameLayer::SpriteAnimateFrameLayer()中完成如下步骤 1.读取素材文件 CCTexture2D* textureCCTextureCache::sharedTextureCache()-addImage(Sprite/PlayerRun.png); 2.记录单帧信息 1 CCSpriteFrame* frame0CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*0,0,140,140));2 CCSpriteFrame* frame1CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*1,0,140,140));3 CCSpriteFrame* frame2CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*2,0,140,140));4 CCSpriteFrame* frame3CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*3,0,140,140));5 CCSpriteFrame* frame4CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*4,0,140,140));6 CCSpriteFrame* frame5CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*5,0,140,140));7 CCSpriteFrame* frame6CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*6,0,140,140));8 CCSpriteFrame* frame7CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*7,0,140,140));9 CCSpriteFrame* frame8CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*8,0,140,140));
10 CCSpriteFrame* frame9CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*9,0,140,140));
11 CCSpriteFrame* frame10CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*10,0,140,140));
12 CCSpriteFrame* frame11CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*11,0,140,140)); 3.设置起始帧 1 CCSprite* spriteCCSprite::spriteWithSpriteFrame(frame1);
2 sprite-setPosition(ccp(s.width/2,s.height/2));
3 this-addChild(sprite,1); 4.生成逐帧数组 1 CCMutableArrayCCSpriteFrame** animFramesnew CCMutableArrayCCSpriteFrame*(12);2 animFrames-addObject(frame0);3 animFrames-addObject(frame1);4 animFrames-addObject(frame2);5 animFrames-addObject(frame3);6 animFrames-addObject(frame4);7 animFrames-addObject(frame5);8 animFrames-addObject(frame6);9 animFrames-addObject(frame7);
10 animFrames-addObject(frame8);
11 animFrames-addObject(frame9);
12 animFrames-addObject(frame10);
13 animFrames-addObject(frame11); 5.执行动画 1 CCAnimation* animationCCAnimation::animationWithFrames(animFrames,0.1f);//Animation动画信息
2 animFrames-release();
3 CCAnimate* animateCCAnimate::actionWithAnimation(animation,false);
4 CCActionInterval* seq(CCActionInterval*)(CCSequence::actions(animate,NULL));//动画间隔
5 sprite-runAction(CCRepeatForever::actionWithAction(animate)); 创建动画帧集合CCSpriteBatchNode使用 对于通过逐帧数组播放动画同样可以通过CCSpriteBatchNode实现。 动画帧集合即是导入贴图文件.png和导入贴图文件的配置文件.plist 前面都是通过一张图片或图片的部分创建精灵并将其加入到场景中这样导致的结果是每次添加精灵时都要逐个渲染精灵性能低下一旦过多精灵渲染将会影响效率。 精灵批处理 使用CCSpriteBatchNode来批处理这些精灵就可以避免帧率的下降。 创建CCSpriteBatchNode的方法 static CCSpriteBatchNode* batchNodeWithTexture(CCTexture2D *tex);static CCSpriteBatchNode* batchNodeWithTexture(CCTexture2D* tex, unsigned int capacity);static CCSpriteBatchNode* batchNodeWithFile(const char* fileImage);static CCSpriteBatchNode* batchNodeWithFile(const char* fileImage, unsigned int capacity); 其中capacity是子节点的数量当然不显式设置子节点的数量的话系统会使用默认值29在运行时如果超过空间了会增加33%的容量。 CCSpriteBatchNode有一个限制就是所使用的图片必须来自同一个文件如果使用一张图片来创建精灵你将不能指定精灵的深度这样所有的精灵都必须在同一渲染层不过你可以使用贴图集来避免这个问题如果你的所有贴图都在同一个文件里那么你只需创建一个CCSpriteBatchNode就可以了。贴图的大小必须满足2的n次方。 将素材文件png和plist文件添加到Sprite/Plist/中 RoleRun.plist代码 RoleRun.plist 1 ?xml version1.0 encodingUTF-8?2 !DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd3 plist version1.04 dict5 keyframes/key6 dict7 keyRoleRun0.png/key8 dict9 keyframe/key10 string{{0,0},{100,124}}/string11 keyoffset/key12 string{-4,-7}/string13 keyrotated/key14 false/15 keysourceColorRect/key16 string{{16,15},{100,124}}/string17 keysourceSize/key18 string{140,140}/string19 /dict20 keyRoleRun1.png/key21 dict22 keyframe/key23 string{{100,0},{92,118}}/string24 keyoffset/key25 string{1,-3}/string26 keyrotated/key27 false/28 keysourceColorRect/key29 string{{25,14},{92,118}}/string30 keysourceSize/key31 string{140,140}/string32 /dict33 keyRoleRun2.png/key34 dict35 keyframe/key36 string{{192,0},{104,112}}/string37 keyoffset/key38 string{1,-1}/string39 keyrotated/key40 false/41 keysourceColorRect/key42 string{{19,15},{104,112}}/string43 keysourceSize/key44 string{140,140}/string45 /dict46 keyRoleRun3.png/key47 dict48 keyframe/key49 string{{296,0},{110,114}}/string50 keyoffset/key51 string{-2,-2}/string52 keyrotated/key53 false/54 keysourceColorRect/key55 string{{13,15},{110,114}}/string56 keysourceSize/key57 string{140,140}/string58 /dict59 keyRoleRun4.png/key60 dict61 keyframe/key62 string{{406,0},{112,118}}/string63 keyoffset/key64 string{-6,-5}/string65 keyrotated/key66 false/67 keysourceColorRect/key68 string{{8,16},{112,118}}/string69 keysourceSize/key70 string{140,140}/string71 /dict72 keyRoleRun5.png/key73 dict74 keyframe/key75 string{{518,0},{98,118}}/string76 keyoffset/key77 string{-7,-6}/string78 keyrotated/key79 false/80 keysourceColorRect/key81 string{{14,17},{98,118}}/string82 keysourceSize/key83 string{140,140}/string84 /dict85 keyRoleRun6.png/key86 dict87 keyframe/key88 string{{616,0},{102,122}}/string89 keyoffset/key90 string{-3,-5}/string91 keyrotated/key92 false/93 keysourceColorRect/key94 string{{16,14},{102,122}}/string95 keysourceSize/key96 string{140,140}/string97 /dict98 keyRoleRun7.png/key99 dict
100 keyframe/key
101 string{{718,0},{96,118}}/string
102 keyoffset/key
103 string{2,-1}/string
104 keyrotated/key
105 false/
106 keysourceColorRect/key
107 string{{24,12},{96,118}}/string
108 keysourceSize/key
109 string{140,140}/string
110 /dict
111 keyRoleRun8.png/key
112 dict
113 keyframe/key
114 string{{814,0},{96,118}}/string
115 keyoffset/key
116 string{0,-1}/string
117 keyrotated/key
118 false/
119 keysourceColorRect/key
120 string{{22,12},{96,118}}/string
121 keysourceSize/key
122 string{140,140}/string
123 /dict
124 keyRoleRun9.png/key
125 dict
126 keyframe/key
127 string{{910,0},{100,118}}/string
128 keyoffset/key
129 string{-2,-2}/string
130 keyrotated/key
131 false/
132 keysourceColorRect/key
133 string{{18,13},{100,118}}/string
134 keysourceSize/key
135 string{140,140}/string
136 /dict
137 /dict
138 keymetadata/key
139 dict
140 keyformat/key
141 integer2/integer
142 keyrealTextureFileName/key
143 stringRoleRun.png/string
144 keysize/key
145 string{2048,128}/string
146 keysmartupdate/key
147 string$TexturePacker:SmartUpdate:43e6d77d8691aadfa1c598803e171096$/string
148 keytextureFileName/key
149 stringRoleRun.png/string
150 /dict
151 /dict
152 /plist RoleRun.png图 1.创建批处理节点读取plist文件 //创建批处理节点读取plist文件CCSpriteFrameCache* cacheCCSpriteFrameCache::sharedSpriteFrameCache();cache-addSpriteFramesWithFile(Sprite/Plist/RoleRun.plist,Sprite/Plist/RoleRun.png); 2. 起始精灵 1 CCSprite* sprite1CCSprite::spriteWithSpriteFrameName(RoleRun0.png);//纹理plist中包含RoleRun0.png
2 sprite1-setPosition(ccp(s.width/2,s.height/4));
3
4 CCSpriteBatchNode* spritebatch CCSpriteBatchNode::batchNodeWithFile(Sprite/Plist/RoleRun.png);//与CCSpriteFrameCache同一纹理
5 spritebatch-addChild(sprite1);
6 addChild(spritebatch); 其中 RoleRun0.png 为plist文件中的节点纹理 3.创建逐帧数组 1 //创建逐帧数组
2 CCMutableArrayCCSpriteFrame** animFrames1new CCMutableArrayCCSpriteFrame*(10);
3 char str1[100]{0};
4 for(int i0;i10;i)
5 {
6 sprintf(str1,RoleRun%d.png,i);
7 CCSpriteFrame* pFramecache-spriteFrameByName( str1 );
8 animFrames1-addObject(pFrame);
9 } 4.执行动画 1 CCAnimation* animation1CCAnimation::animationWithFrames(animFrames1,0.2);
2 sprite1-runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false))); 这样同样可以实现同样的效果 运行效果两个Sprite都能动起来 SpriteAnimationTest.h完整代码 SpriteAnimationTest.h 1 #ifndef _SPRITE_ANIMATION_TEST_2 #define _SPRITE_ANIMATION_TEST_3 4 #include cocos2d.h5 6 using namespace cocos2d;7 8 class SpriteAnimateFrameScene:public CCScene9 {
10 public:
11 SpriteAnimateFrameScene();
12 ~SpriteAnimateFrameScene();
13
14 virtual void onEnter();
15 };
16
17 class SpriteAnimateFrameLayer:public CCLayer
18 {
19 public:
20 SpriteAnimateFrameLayer();
21 ~SpriteAnimateFrameLayer();
22 };
23
24 #endif //_SPRITE_ANIMATION_TEST_ SpriteAnimationTest.cpp完整代码 SpriteAnimationTest.cpp 1 #include pch.h2 #include Classes\SpriteAnimationTest.h3 4 //www.cnblogs.com/suguoqiang5 //---------------------------------------6 //7 //SpriteAnimateFrameLayer8 //9 //---------------------------------------10 11 SpriteAnimateFrameLayer::SpriteAnimateFrameLayer()12 {13 CCSize sCCDirector::sharedDirector()-getWinSize();14 //CCSpriteFrame15 //读取素材文件16 CCTexture2D* textureCCTextureCache::sharedTextureCache()-addImage(Sprite/PlayerRun.png);17 //记录单帧信息18 CCSpriteFrame* frame0CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*0,0,140,140));19 CCSpriteFrame* frame1CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*1,0,140,140));20 CCSpriteFrame* frame2CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*2,0,140,140));21 CCSpriteFrame* frame3CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*3,0,140,140));22 CCSpriteFrame* frame4CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*4,0,140,140));23 CCSpriteFrame* frame5CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*5,0,140,140));24 CCSpriteFrame* frame6CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*6,0,140,140));25 CCSpriteFrame* frame7CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*7,0,140,140));26 CCSpriteFrame* frame8CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*8,0,140,140));27 CCSpriteFrame* frame9CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*9,0,140,140));28 CCSpriteFrame* frame10CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*10,0,140,140));29 CCSpriteFrame* frame11CCSpriteFrame::frameWithTexture(texture,CCRectMake(140*11,0,140,140));30 // 起始帧31 CCSprite* spriteCCSprite::spriteWithSpriteFrame(frame1);32 sprite-setPosition(ccp(s.width/2,s.height/2));33 this-addChild(sprite,1);34 //生成逐帧数组35 CCMutableArrayCCSpriteFrame** animFramesnew CCMutableArrayCCSpriteFrame*(12);36 animFrames-addObject(frame0);37 animFrames-addObject(frame1);38 animFrames-addObject(frame2);39 animFrames-addObject(frame3);40 animFrames-addObject(frame4);41 animFrames-addObject(frame5);42 animFrames-addObject(frame6);43 animFrames-addObject(frame7);44 animFrames-addObject(frame8);45 animFrames-addObject(frame9);46 animFrames-addObject(frame10);47 animFrames-addObject(frame11);48 //动画Animate49 CCAnimation* animationCCAnimation::animationWithFrames(animFrames,0.1f);//Animation动画信息50 animFrames-release();51 CCAnimate* animateCCAnimate::actionWithAnimation(animation,false);52 CCActionInterval* seq(CCActionInterval*)(CCSequence::actions(animate,NULL));//动画间隔53 sprite-runAction(CCRepeatForever::actionWithAction(animate)); 54 55 //CCSpriteBatchNode56 //创建批处理节点读取plist文件57 CCSpriteFrameCache* cacheCCSpriteFrameCache::sharedSpriteFrameCache();58 cache-addSpriteFramesWithFile(Sprite/Plist/RoleRun.plist,Sprite/Plist/RoleRun.png);59 60 //起始精灵61 CCSprite* sprite1CCSprite::spriteWithSpriteFrameName(RoleRun0.png);//纹理plist中包含RoleRun0.png62 sprite1-setPosition(ccp(s.width/2,s.height/4)); 63 64 CCSpriteBatchNode* spritebatch CCSpriteBatchNode::batchNodeWithFile(Sprite/Plist/RoleRun.png);//与CCSpriteFrameCache同一纹理65 spritebatch-addChild(sprite1);66 addChild(spritebatch);67 68 //创建逐帧数组69 CCMutableArrayCCSpriteFrame** animFrames1new CCMutableArrayCCSpriteFrame*(10);70 char str1[100]{0};71 for(int i0;i10;i)72 {73 sprintf(str1,RoleRun%d.png,i);74 CCSpriteFrame* pFramecache-spriteFrameByName( str1 );75 animFrames1-addObject(pFrame);76 }77 78 CCAnimation* animation1CCAnimation::animationWithFrames(animFrames1,0.2);79 sprite1-runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false)));80 81 animFrames-release();82 animFrames1-release();83 }84 85 SpriteAnimateFrameLayer::~SpriteAnimateFrameLayer()86 {}87 88 //---------------------------------------89 //90 //SpriteAnimateFrameScene91 //92 //---------------------------------------93 94 SpriteAnimateFrameScene::SpriteAnimateFrameScene()95 {}96 97 SpriteAnimateFrameScene::~SpriteAnimateFrameScene()98 {}99
100 void SpriteAnimateFrameScene::onEnter()
101 {
102 CCScene::onEnter();
103 CCLayer* pLayernew SpriteAnimateFrameLayer();
104 this-addChild(pLayer);
105 pLayer-release();
106 }
107
108 //www.cnblogs.com/suguoqiang 著作权声明本文由http://www.cnblogs.com/suguoqiang 原创欢迎转载分享。请尊重作者劳动转载时保留该声明和作者博客链接谢谢 转载于:https://www.cnblogs.com/suguoqiang/archive/2013/04/03/2997316.html