天天看點

cocos2d

在cocos2d-x提供的工程樣例(HelloWorld)的init函數中添加以下功能:

功能1:實作精靈的動作

圖檔資源:

​​

cocos2d

​player.png

代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

​// 得到裝置螢幕的大小​

​CCSize s = CCDirector::sharedDirector()->getWinSize(); ​

​// 加載精靈圖檔​

​CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(​

​"player.png"​

​);​

​// 建立精靈角色的幀紋理,四個方向​

​CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(32*0, 48*0, 32, 48));​

​CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(32*1, 48*0, 32, 48));​

​CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(32*2, 48*0, 32, 48));​

​CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(32*3, 48*0, 32, 48));​

​// 把精靈角色的幀紋理加到動畫管理器中​

​CCMutableArray<CCSpriteFrame*> *animFrames = ​

​new​

​ ​

​CCMutableArray<CCSpriteFrame*>(4);​

​animFrames->addObject(frame0);​

​animFrames->addObject(frame1);​

​animFrames->addObject(frame2);​

​animFrames->addObject(frame3);​

​// 建立精靈角色的動作對象,這裡設定成0.2秒變幀​

​CCAnimation *animation = CCAnimation::animationWithFrames(animFrames, 0.2f);​

​// 将管理器中的資源釋放掉​

​animFrames->release();​

​// 建立精靈對象,這裡把圖檔資源的第一行元素目前預設動畫效果​

​CCSprite *sprite = CCSprite::spriteWithSpriteFrame(frame0);​

​// 設定精靈對象在螢幕上的位置,​

​sprite->setPosition(ccp(s.width/2, s.height/2));​

​// 把精靈對象添加到顯示層中​

​this​

​->addChild(sprite);​

​// 建立動畫的執行個體​

​CCAnimate *animate = CCAnimate::actionWithAnimation(animation, ​

​false​

​);​

​// 讓精靈開始動畫​

​sprite->runAction(CCRepeatForever::actionWithAction(animate));​

效果圖:

cocos2d

功能2:實作遮罩效果

cocos2d

​​ light.png

代碼(緊跟功能1):

首先實作這麼一個函數:

​​​​

​//************************************​

​// Method:    spriteWithFile​

​// FullName:  spriteWithFile​

​// Access:    public ​

​// Returns:   cocos2d::CCSprite *​

​// Qualifier: 根據檔案名得到檔案資源​

​// Parameter: const char * filename​

​//************************************​

​cocos2d::CCSprite *spriteWithFile(​

​const​

​char​

​*filename)​

​{​

​cocos2d::CCSprite *pobSprite = ​

​new​

​CCSprite();​

​if​

​(pobSprite && pobSprite->initWithFile(filename))​

​{​

​pobSprite->autorelease();    ​

​// 把資源的釋放權交給引擎​

​return​

​pobSprite;           ​

​// 這樣就得到綁定指定資源的角色對象​

​}​

​CC_SAFE_DELETE(pobSprite);​

​return​

​NULL;​

​}​

在init函數中追加:

​// 為光圈建立一個執行個體​

​CCSprite *sprite1 = CCSprite::spriteWithFile(​

​"light.png"​

​);​

​// 設定光圈的位置​

​sprite1->setPosition(ccp(100, 100));​

​// 把光圈添加到顯示層中​

​this​

​->addChild(sprite1, 2);​

​// 設定光圈的動畫​

​sprite1->runAction(CCRepeatForever::actionWithAction(​

​// 将光圈的動畫設定成序列化,采用雙補間動畫方式​

​(CCActionInterval *)CCSequence::actions(CCMoveBy::actionWithDuration(3.0f, ccp(300, 0)), ​

​CCMoveBy::actionWithDuration(0.1f, ccp(-300,0)), NULL)));​

​// 建立遮罩對象​

​darknessLayer = CCRenderTexture::renderTextureWithWidthAndHeight(s.width, s.height);​

​// 裝置遮罩的位置​

​darknessLayer->setPosition(ccp(s.width/2, s.height/2));​

​// 将遮罩效果加到顯示層中​

​this​

​->addChild(darknessLayer, 20);​

​// 定義遮罩效果的色彩​

​darknessLayer->clear(0,0,0,0.5f);​

最後在HelloWorld中複寫draw方法如下:

​void​

​HelloWorld::draw()​

​{​

​// 恢複遮罩效果的色彩​

​darknessLayer->clear(0,0,0,0.5f);​

​cocos2d::CCLayer::draw();​

​}​

cocos2d