对于CoCos中的节点默认是会相互覆盖的,但是有时我们也会用到层叠颜色和透明度的时候,这个时候我们就需要借助CCRGBAProtocol中提供的接口来进行实现,可以看出CCSPrite和CCLayer均实现了这个接口,这里我觉得需要大家在开发中注意的问题是该属性是需要递归设置才能体现其价值的。这里我们来看一段CCRGBAProtocal的源代码:
/** * whether or not color should be propagated to its children. */ virtual bool isCascadeColorEnabled(void) = 0; virtual void setCascadeColorEnabled(bool cascadeColorEnabled) = 0;
/** * whether or not opacity should be propagated to its children. */ virtual bool isCascadeOpacityEnabled(void) = 0; virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) = 0;
static void setEnableRecursiveCascading(CCNode* node, bool enable) { CCRGBAProtocol* rgba = dynamic_cast<CCRGBAProtocol*>(node); if (rgba) { rgba->setCascadeColorEnabled(enable); rgba->setCascadeOpacityEnabled(enable); } CCObject* obj; CCArray* children = node->getChildren(); CCARRAY_FOREACH(children, obj) { CCNode* child = (CCNode*)obj; setEnableRecursiveCascading(child, enable); } }