Cocos2d-x 2.1用png创建序列帧动画
cocos2d吧
全部回复
仅看楼主
level 7
策划狮 楼主
序列帧动画主要有几个类:
CCSpriteFrame:精灵帧信息,序列帧动画是依靠多个精灵帧信息来显示相应的纹理图像,一个精灵帧信息包包含了所使用的纹理,对应纹理块的位置以及纹理块是否经过旋转和偏移,这些信息可以取得对应纹理中
正确的
纹理块区域做为精灵帧显示的图像。
CCAnimationFrame:序列帧动画单帧信息,它存储了对应的精灵帧信息。
CCAnimation:序列帧动画信息,它存储了所有的单帧信息,可以对单帧信息进行管理。
CCAnimate:序列帧动画处理类,它是真正完成动画表演的类。
创建流程是先创建动画帧信息,然后由动画帧信息生成动画信息,最后由动画信息创建出序列帧动画供精灵演示。示例代码如下:
//将图片生成纹理,保存到全局的纹理缓存取
CCTexture2D *heroTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
//用纹理创建4幅帧动画
CCSpriteFrame *frame0,*frame1,*frame2,*frame3;
//第二个参数表示显示区域的x,y,width,height
frame0=CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(32*0,32*1,32,32));
frame1=CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(32*1,32*1,32,32));
frame2=CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(32*2,32*1,32,32));
frame3=CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(32*3,32*1,32,32));
//CCMutableArray<CCSpriteFrame*> *animFramess=new CCMutableArray<CCSpriteFrame*>(4);
CCArray *animFrames=CCArray::create();
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
//根据4幅帧生成CCAnimation对象
CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames);
//创建一个CCSprite用来显示勇士,可以使用Animation中的一帧来作为勇士静止时的画面
CCSprite *heroSprite=CCSprite::createWithSpriteFrame(frame0);
//暂时将勇士显示在(100,100)处
heroSprite->setPosition(ccp(100, 100));
addChild(heroSprite);
//根据动画模板创建动画
animation->setDelayPerUnit(0.2f);
CCAnimate *animate=CCAnimate::create(animation);
//创建不断重复的动画,并让heroSprite播放
heroSprite->runAction(CCRepeatForever::create(animate));
2014年08月12日 07点08分 1
level 1
为什么出错呢?
2014年09月20日 08点09分 2
1