Cocos2d-x--CCAnimate实现简单动画

所用到的图片:

Cocos2d-x--CCAnimate实现简单动画_第1张图片

 

实现动画步骤:

1.读取2D纹理信息

2.记录单帧信息

3.生成逐帧数组

4.设置动画信息

5.设置起始帧

6.创建并执行动画

 

实现:新建一个Cocos2d-x项目,将图片放到工程资源中,修改HelloWorldSence.cpp中do代码块中的代码

		CC_BREAK_IF(! CCLayer::init());

		// Get window size and place the label upper. 
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		// 1.读取2D纹理信息
		CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("items.png");

		//2.记录单帧信息
		CCSpriteFrame* m_frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*0, 128, 32, 32*/64, 160, 64, 16));
		CCSpriteFrame* m_frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*32, 128, 32, 32*/ 64, 176, 64, 16));
		CCSpriteFrame* m_frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*64, 128, 32, 32*/64, 192, 64, 16));
		CCSpriteFrame* m_frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(/*96, 128, 32, 32*/64, 208, 64, 16));

		//3.生成逐帧数组
		CCArray *animFrames = CCArray::create();
		CC_BREAK_IF(!animFrames);
		animFrames->addObject(m_frame1);
		animFrames->addObject(m_frame2);
		animFrames->addObject(m_frame3);
		animFrames->addObject(m_frame4);

		//4.动画信息,设置间隔时间为0.5
		CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5f);
		CC_BREAK_IF(! animation);

		//5.设置起始帧
		CCSprite *hero = CCSprite::createWithSpriteFrame(m_frame1);
		CC_BREAK_IF(!hero);
		hero->setPosition(ccp(size.width/2, size.height/2));
		this->addChild(hero, 0);

		//6.创建并执行动画
		CCAnimate *animate = CCAnimate::create(animation);
		hero->runAction(CCRepeatForever::create(animate));

		bRet = true;


 

你可能感兴趣的:(cocos2d-x,ccanimation,runAction,CCAnimate)