实现淡入淡出效果的蒙版

[color=red][size=medium][b] 这是一个系列的文章,详情可点击[url=http://lizi07.iteye.com/blog/2104106]关于这两年所经历项目的系列总结[/url][/b][/size][/color]

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

#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


[size=medium].cpp文件如下[/size]

#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);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值