cocos2d-之动画图片加载

来源:互联网 发布:mac怎么停止更新系统 编辑:程序博客网 时间:2024/04/29 04:52

 

下面在的代码添加在init()函数中。实例以这个图片加载为例子。

需要这个图片资源,点击这里下载http://pan.baidu.com/s/1hqn96Qw

 /*************************************************************/
   const int sum = 16;//设置图片的动作帧数
   //加载动作图片
   CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("F:/man2.png");

   int cx = texture->getContentSize().width;//获取图片的宽度
   int cy = texture->getContentSize().height;//获取图片的高度

   //设置sprite精灵显示的一幅图和位置。并把图片显示到窗格内容中
   CCSprite* sprite = CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 0, cx/sum, cy)));
   sprite->setPosition( ccp( 200, 200) );
   addChild(sprite,1,912);//912是这个精灵的标识。

      CCArray *pAnimateFrames = CCArray::create();//创建数组,把每幅动画图片添加到数组里面。
      for(int i = 0 ;i != sum ;i++)
   {
       pAnimateFrames->addObject(CCSpriteFrame::createWithTexture(texture, CCRectMake((cx/sum)*i, 0, cx/sum, cy)));
   }
   CCAnimation *animation  = CCAnimation::createWithSpriteFrames(pAnimateFrames, 0.05f);//设置切换图片的时间。

  //因为这个动画的帧数比较多,所以切换的时间设置的比较短。
   CCAnimate *animate = CCAnimate::create(animation);//创建动画、
   sprite->runAction(CCRepeatForever::create(animate));//启动动画、
  /*************************************************************/

如上面有代码错误,不吝提出。

下面是效果图

0 0
原创粉丝点击