使用plist创建精灵并执行动画

来源:互联网 发布:洛阳驱逐舰数据 编辑:程序博客网 时间:2024/06/05 23:16

 素材图:

然后使用Texture Packer放到一张图片上,并生成plist文件


接下来直接上代码

[cpp] view plaincopy
  1. CCSize size = CCDirector::sharedDirector()->getWinSize();  //获得屏幕的尺寸  
  2.   
  3. CCArray* pArray = CCArray::createWithCapacity(4);  
  4. CCSpriteFrameCache* pCache = CCSpriteFrameCache::sharedSpriteFrameCache();  //获得SpriteFrame的缓存  
  5. pCache->addSpriteFramesWithFile("attack.plist");  
  6. CCSprite* pSprite = CCSprite::createWithSpriteFrameName("A1_0.png");  //用attack.png中的第一张图片创建一个精灵  
  7. pSprite->setPosition(ccp(size.width/2,size.height/2));  //设置精灵的坐标  
  8.   
  9.   
  10. char name[20];  
  11. for(int i=0; i<4; i++)  //将plist中的图片都加载,放到array中  
  12. {  
  13.     sprintf(name, "A1_%d.png", i);  
  14.     CCSpriteFrame* pframe = pCache->spriteFrameByName(name);  
  15.     pArray->addObject(pframe);  
  16. }  
  17.   
  18. CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.2f);  //用array中的spriteframe创建一个animation,每帧间隔0.2s  
  19. CCAnimate* panimate = CCAnimate::create(pAnimation);  
  20.   
  21. pSprite->runAction(CCRepeatForever::create(panimate));  


运行结果就是


在mac下面qq截图不能截动态的,就给个样子,大家自己写个demo试试就可以看到效果。

更多0
0 0