How to do Fadein and FadeOut on CCLayer

I kown if I implement CCRGBAProtocol and overwrite setOpacity ,I can do FadeIn and FadeOut on CCLayer in cocos2d-iphone.
But it doesn’t work in cocos2d-x!
Anyone has did this in cocos2d-x??

This works for me:
In CCTMXLayer.h

public:
virtual void setColor(const ccColor3B& color)
{
// TODO
}

/*** returns the color
**since v0.8
/
virtual const ccColor3B& getColor
{
// TODO
return ccWHITE;
}
// returns the opacity
virtual GLubyte getOpacity
{
return m_cOpacity;
}
/
* sets the opacity.
**warning If the the texture has premultiplied alpha then, the R, G and B channels will be modifed.
Values goes from 0 to 255, where 255 means fully opaque.
**/
virtual void setOpacity(GLubyte opacity);

// optional

/*** sets the premultipliedAlphaOpacity property.
If set to NO then opacity will be applied as: glColor;
If set to YES then oapcity will be applied as: glColor;
Textures with premultiplied alpha will have this property by default on YES. Otherwise the default value is NO
**since v0.8
/
virtual void setIsOpacityModifyRGB
{
// TODO
}
/
* returns whether or not the opacity will be applied using glColor(R,G,B,opacity) or glColor(opacity, opacity, opacity, opacity);
**since v0.8
**/
virtual bool getIsOpacityModifyRGB(void)
{
// TODO
return false;
}

In CCTMXLayer.cpp:

void CCTMXLayer::setOpacity(GLubyte opacity)
{
int total = m_pobTextureAtlas->getTotalQuads();

//default color is white, premultiply it with alpha
//if you need to blend your map with a different color, change it here
ccColor4B c = {255 * opacity / 255, 255 * opacity / 255, 255 * opacity / 255, opacity};

for (int i = 0; i < total; ++i)
{
ccV3F_C4B_T2F_Quad quad = m_pobTextureAtlas~~>getAndDirtyQuadAtIndex;
quad~~>bl.colors = c;
quad~~>tl.colors = c;
quad~~>br.colors = c;
quad->tr.colors = c;
}
m_cOpacity = opacity;
}
In CCTextureAtlas.h:
ccV3F_C4B_T2F_Quad
getAndDirtyQuadAtIndex(unsigned int index);

In CCTextureAtlas.cpp:

ccV3F_C4B_T2F_Quad* CCTextureAtlas::getAndDirtyQuadAtIndex(unsigned int index)
{
CCAssert(index < m_uTotalQuads && index >= 0, “getQuadAtIndex: Invalid index”);
#if CC_USES_VBO
m_bDirty = true;
#endif

return &(m_pQuads[index]);
}

How can I put all this in one paragraph? :slight_smile:

I think one of the problems of implementing the CCRGBAProtocol on a CCLayer is that, as it’s usually used as a collection of CCSprites (and other nodes), each of them may have their own values for opacity and color. There would be no unique values to return on the getOpacity and getColor functions for a CCLayer. CCLayerColor do implement those functions, but it ignores completely their children. It would be nice to have a function to handle opacity for all children of a CCLayer, maybe there is an easier approach without the need to implement this. But as we’re on this topic, I just had this need and ended up making a version of that workaround on cocos2d-x. As follows:

/* CCRGBALayer.h /
#include “cocos2d.h”
class CCRGBALayer : public cocos2d::CCLayer, public cocos2d::CCRGBAProtocol {
/
Opacity: conforms to CCRGBAProtocol protocol /
CC_PROPERTY
/
Color: conforms with CCRGBAProtocol protocol /
CC_PROPERTY_PASS_BY_REF;
public:
static CCRGBALayer
create();
virtual void setOpacityModifyRGB(bool bValue);
virtual bool isOpacityModifyRGB(void);
protected:
bool m_bOpacityModifyRGB;
};

/* CCRGBALayer.cpp****/
#include “CCRGBALayer.h”
using namespace cocos2d;
CCRGBALayer
CCRGBALayer::create()
{
CCRGBALayer pRet = new CCRGBALayer;
if )
{
pRet~~>autorelease;
pRet~~>m_bOpacityModifyRGB = true;
pRet~~>m_nOpacity = 255;
pRet~~>m_sColor = ccWHITE;
return pRet;
}
else
{
CC_SAFE_DELETE;
return NULL;
}
}
GLubyte CCRGBALayer::getOpacity{
return m_nOpacity;
}
void CCRGBALayer::setOpacity
{
m_nOpacity = opacity;
CCObject
child;
CCARRAY_FOREACH(m_pChildren, child)
{
CCNode* pNode = (CCNode**) child;
CCRGBAProtocolpRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pNode);
if (pRGBAProtocol)
{
pRGBAProtocol~~>setOpacity;
}
}
}
ccColor3B CCRGBALayer::getColor
{
return m_sColor;
}
void CCRGBALayer::setColor
{
m_sColor = color3;
CCObject* child;
CCARRAY_FOREACH
{
CCNode* pNode = child;
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>;
if
{
pRGBAProtocol~~>setColor(color3);
}
}
}
void CCRGBALayer::setOpacityModifyRGB(bool bValue)
{
m_bOpacityModifyRGB = bValue;
CCObject* child;
CCARRAY_FOREACH(m_pChildren, child)
{
CCNode* pNode = (CCNode
) child;
CCRGBAProtocol**pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pNode);
if (pRGBAProtocol)
{
pRGBAProtocol->setOpacityModifyRGB(bValue);
}
}
}
bool CCRGBALayer::isOpacityModifyRGB(void)
{
return m_bOpacityModifyRGB;
}

What this class does is to propagate setOpacity, setColor and setOpacityModifyRGB functions to all it’s children whenever they are called. The CCRGBALayer keeps track of the last value used and returns them on the getters, but be aware of the fact that it does not guarantee that all it’s children will have the same values, as they can be modified on it’s own.

It may not be the best solution, but I’m quite satisfied with this behaviour.
If there’s anything wrong or any side-effects that I may be unaware of in this modification, please let me know. I can’t say I’m an expert.