实现淡入淡出效果的蒙版

接下来要做的第一个小功能就是,实现一个有淡入淡出效果的蒙版。cocos2dx里面的蒙版有CCLayerColor,直接使用会比较生硬。这里使用CCFade动画来给它实现一个淡入淡出的效果。 
这个类给我它取名GameMaskLayer。 

.h文件如下  
  1. #ifndef _GAME_MASK_LAYER_H_  
  2. #define  _GAME_MASK_LAYER_H_  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. USING_NS_CC;  
  7.   
  8. class GameMaskLayer : public CCLayerColor  
  9. {  
  10. public:  
  11.     enum {MaskLayerZOder=-100, MaskLayerTag=100, FadeOutTag=101, FadeInTag=102};  
  12.     GameMaskLayer(void);  
  13.     ~GameMaskLayer(void);  
  14.       
  15.     static GameMaskLayer* create(ccColor4B color = ccc4(0, 0, 0, 100));  
  16.     static GameMaskLayer* create(CCNode* pNode, ccColor4B color = ccc4(0, 0, 0, 100));  
  17.     static GameMaskLayer* create(CCNode* pNode, int zOrder, ccColor4B color = ccc4(0, 0, 0, 100));  
  18.     static GameMaskLayer* create(CCNode* pNode, int zOrder, int tag, ccColor4B color = ccc4(0, 0, 0, 100));  
  19.     virtual bool initLayer(CCNode* pNode, int zOrder, int tag, ccColor4B color);  
  20.     void doFadeOut(CCNode* pParent);  
  21.   
  22. private:  
  23.     void onFadeInEnded(CCNode* pParent);  
  24.     void onFadeOutEnded(CCNode* pParent);  
  25.     virtual void visit();  
  26.   
  27. private:  
  28.     GLubyte mOpacity;  
  29.     static GameMaskLayer* mInstance;  
  30. };  
  31. #endif  


.cpp文件如下  
  1. #include "GameMaskLayer.h"  
  2.   
  3. GameMaskLayer::GameMaskLayer(void)  
  4. {  
  5.     mOpacity = 0;  
  6. }  
  7.   
  8. GameMaskLayer::~GameMaskLayer(void)  
  9. {  
  10. }  
  11.   
  12. GameMaskLayer* GameMaskLayer::create(ccColor4B color)  
  13. {  
  14.     return create(NULL, MaskLayerZOder, MaskLayerTag, color);  
  15. }  
  16.   
  17. GameMaskLayer* GameMaskLayer::create(CCNode* pNode, ccColor4B color)  
  18. {  
  19.     return create(pNode, MaskLayerZOder, MaskLayerTag, color);  
  20. }  
  21.   
  22. GameMaskLayer* GameMaskLayer::create(CCNode* pNode, int zOrder, ccColor4B color)  
  23. {  
  24.     return create(pNode, zOrder, MaskLayerTag, color);  
  25. }  
  26.   
  27. GameMaskLayer* GameMaskLayer::create(CCNode* pNode, int zOrder, int tag, ccColor4B color)  
  28. {  
  29.     GameMaskLayer *mInstance = new GameMaskLayer();  
  30.     if (mInstance != NULL && mInstance->initLayer(pNode, zOrder, tag, color))  
  31.     {  
  32.         mInstance->autorelease();  
  33.         return mInstance;  
  34.     }  
  35.     CC_SAFE_DELETE(mInstance);  
  36.     return NULL;  
  37. }  
  38.   
  39. bool GameMaskLayer::initLayer(CCNode* pNode, int zOrder, int tag, ccColor4B color)  
  40. {  
  41.     GLubyte oldOpactiy = this->getOpacity();   
  42.   
  43.     if (!CCLayerColor::initWithColor(color)) return false;  
  44.       
  45.     this->setOpacity(oldOpactiy);  
  46.     mOpacity = color.a;  
  47.     this->_setZOrder(zOrder);  
  48.     this->setPosition(ccp(0, 0));  
  49.     this->setTag(tag);  
  50.   
  51.     if (oldOpactiy != mOpacity)  
  52.     {  
  53.         this->stopActionByTag(FadeOutTag);  
  54.         this->stopActionByTag(FadeInTag);  
  55.   
  56.         CCAction *pAction = CCSequence::create(CCFadeTo::create(0.2f, mOpacity),  
  57.                                             CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeInEnded)), NULL);  
  58.         pAction->setTag(FadeInTag);  
  59.         this->runAction(pAction);  
  60.     }  
  61.   
  62.     if (pNode != NULL)  
  63.     {  
  64.         pNode->addChild(this, zOrder);  
  65.     }else {  
  66.         return false;  
  67.     }  
  68.   
  69.     return true;  
  70. }  
  71.   
  72. void GameMaskLayer::doFadeOut(CCNode* pParent)  
  73. {  
  74.     GLubyte oldOpactiy = this->getOpacity();  
  75.     if(oldOpactiy > 10)  
  76.     {  
  77.         this->stopActionByTag(FadeOutTag);  
  78.         this->stopActionByTag(FadeInTag);  
  79.         CCFiniteTimeAction* pAction = CCSequence::create(CCFadeTo::create(0.2f, 0),   
  80.                                                             CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeOutEnded)), NULL);  
  81.         pAction->setTag(FadeOutTag);  
  82.         this->runAction(pAction);  
  83.     } else {  
  84.         CCFiniteTimeAction* pAction = CCSequence::create(CCDelayTime::create(0.05f),   
  85.             CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeOutEnded)), NULL);  
  86.         this->runAction(pAction);  
  87.     }  
  88. }  
  89.   
  90. void GameMaskLayer::onFadeInEnded(CCNode* pParent)  
  91. {  
  92.       
  93. }  
  94.   
  95. void GameMaskLayer::onFadeOutEnded(CCNode* pNode)  
  96. {  
  97.     this->stopActionByTag(FadeOutTag);  
  98.     this->stopActionByTag(FadeInTag);  
  99.     this->removeFromParentAndCleanup(true);  
  100. }  
  101.   
  102. void GameMaskLayer::visit()  
  103. {  
  104.     CCNode* pParent = this->getParent();  
  105.     float oldScale = pParent->getScale();  
  106.     float oldScaleX = pParent->getScaleX();  
  107.     float oldScaleY = pParent->getScaleY();  
  108.     this->setScale(2.0f/oldScale);  
  109.     this->setScaleX(2.0f/oldScaleX);  
  110.     this->setScaleY(2.0f/oldScaleY);  
  111.     CCLayerColor::visit();  
  112.     this->setScale(oldScale);  
  113.     this->setScaleX(oldScaleX);  
  114.     this->setScaleY(oldScaleY);  
  115. }  


关于这个功能的实现,相对简单没啥好说的,所以贴出全部代码,自己看吧。如果以后实现的功能更复杂些,就会贴出相关代码,附加自己的实现思路。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值