接下来要做的第一个小功能就是,实现一个有淡入淡出效果的蒙版。cocos2dx里面的蒙版有CCLayerColor,直接使用会比较生硬。这里使用CCFade动画来给它实现一个淡入淡出的效果。
这个类给我它取名GameMaskLayer。
.h文件如下
#ifndef _GAME_MASK_LAYER_H_ #define _GAME_MASK_LAYER_H_ #include "cocos2d.h" USING_NS_CC; class GameMaskLayer : public CCLayerColor { public: enum {MaskLayerZOder=-100, MaskLayerTag=100, FadeOutTag=101, FadeInTag=102}; GameMaskLayer(void); ~GameMaskLayer(void); static GameMaskLayer* create(ccColor4B color = ccc4(0, 0, 0, 100)); static GameMaskLayer* create(CCNode* pNode, ccColor4B color = ccc4(0, 0, 0, 100)); static GameMaskLayer* create(CCNode* pNode, int zOrder, ccColor4B color = ccc4(0, 0, 0, 100)); static GameMaskLayer* create(CCNode* pNode, int zOrder, int tag, ccColor4B color = ccc4(0, 0, 0, 100)); virtual bool initLayer(CCNode* pNode, int zOrder, int tag, ccColor4B color); void doFadeOut(CCNode* pParent); private: void onFadeInEnded(CCNode* pParent); void onFadeOutEnded(CCNode* pParent); virtual void visit(); private: GLubyte mOpacity; static GameMaskLayer* mInstance; }; #endif
.cpp文件如下
#include "GameMaskLayer.h" GameMaskLayer::GameMaskLayer(void) { mOpacity = 0; } GameMaskLayer::~GameMaskLayer(void) { } GameMaskLayer* GameMaskLayer::create(ccColor4B color) { return create(NULL, MaskLayerZOder, MaskLayerTag, color); } GameMaskLayer* GameMaskLayer::create(CCNode* pNode, ccColor4B color) { return create(pNode, MaskLayerZOder, MaskLayerTag, color); } GameMaskLayer* GameMaskLayer::create(CCNode* pNode, int zOrder, ccColor4B color) { return create(pNode, zOrder, MaskLayerTag, color); } GameMaskLayer* GameMaskLayer::create(CCNode* pNode, int zOrder, int tag, ccColor4B color) { GameMaskLayer *mInstance = new GameMaskLayer(); if (mInstance != NULL && mInstance->initLayer(pNode, zOrder, tag, color)) { mInstance->autorelease(); return mInstance; } CC_SAFE_DELETE(mInstance); return NULL; } bool GameMaskLayer::initLayer(CCNode* pNode, int zOrder, int tag, ccColor4B color) { GLubyte oldOpactiy = this->getOpacity(); if (!CCLayerColor::initWithColor(color)) return false; this->setOpacity(oldOpactiy); mOpacity = color.a; this->_setZOrder(zOrder); this->setPosition(ccp(0, 0)); this->setTag(tag); if (oldOpactiy != mOpacity) { this->stopActionByTag(FadeOutTag); this->stopActionByTag(FadeInTag); CCAction *pAction = CCSequence::create(CCFadeTo::create(0.2f, mOpacity), CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeInEnded)), NULL); pAction->setTag(FadeInTag); this->runAction(pAction); } if (pNode != NULL) { pNode->addChild(this, zOrder); }else { return false; } return true; } void GameMaskLayer::doFadeOut(CCNode* pParent) { GLubyte oldOpactiy = this->getOpacity(); if(oldOpactiy > 10) { this->stopActionByTag(FadeOutTag); this->stopActionByTag(FadeInTag); CCFiniteTimeAction* pAction = CCSequence::create(CCFadeTo::create(0.2f, 0), CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeOutEnded)), NULL); pAction->setTag(FadeOutTag); this->runAction(pAction); } else { CCFiniteTimeAction* pAction = CCSequence::create(CCDelayTime::create(0.05f), CCCallFuncN::create(this, callfuncN_selector(GameMaskLayer::onFadeOutEnded)), NULL); this->runAction(pAction); } } void GameMaskLayer::onFadeInEnded(CCNode* pParent) { } void GameMaskLayer::onFadeOutEnded(CCNode* pNode) { this->stopActionByTag(FadeOutTag); this->stopActionByTag(FadeInTag); this->removeFromParentAndCleanup(true); } void GameMaskLayer::visit() { CCNode* pParent = this->getParent(); float oldScale = pParent->getScale(); float oldScaleX = pParent->getScaleX(); float oldScaleY = pParent->getScaleY(); this->setScale(2.0f/oldScale); this->setScaleX(2.0f/oldScaleX); this->setScaleY(2.0f/oldScaleY); CCLayerColor::visit(); this->setScale(oldScale); this->setScaleX(oldScaleX); this->setScaleY(oldScaleY); }
关于这个功能的实现,相对简单没啥好说的,所以贴出全部代码,自己看吧。如果以后实现的功能更复杂些,就会贴出相关代码,附加自己的实现思路。