cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient

1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的

2.CCLayerGradient是用来显示颜色渐变效果的图层,它继承自CCLayerColor,是CCLayer的孙类

3.几个特殊的图层:CCLayerColor,CCLayerGradient
  颜色图层在游戏中主要用来烘托背景,可以按照RGB设置填充颜色,同时还可以设置图层的透明度,常用于背景
 
  颜色图层还存在一个特殊的子类:CCLayerGradient,是具有颜色渐变效果的颜色图层
  可以设置背景的渐变效果,Opacity:透明度

4.相关的处理函数:

bool CCLayerColor::initWithColor(const ccColor4B& color);
bool CCLayerColor::initWithColor(const ccColor4B& color, GLfloat w,GLfloat h);

实例:

CCLayerColor::initWithColor(ccc4(255, 255, 255, 255));
CCLayerColor::initWithColor(ccc4(255, 255, 255, 255),100,100);
ignoreAnchorPointForPosition(false);

bool CCLayerGradient::initWithColor(const ccColor4B& start, const ccColor4B& end);
bool CCLayerGradient::initWithColor(const ccColor4B& start, const ccColor4B& end, const CCPoint& v);

实例:

CCLayerGradient::initWithColor(ccc4(123,89,0,255),
ccc4(0,255,255,255),ccp(1,0));

5.代码实现:

.h文件

#ifndef __T04ColorLayer_H__
#define __T04ColorLayer_H__

#include "cocos2d.h"
USING_NS_CC;

/*CCLayer默认的背景颜色是透明的,而CCLayerColor图层,可以设置背景颜色*/
class T04ColorLayer :public CCLayerColor
{
public:
    static CCScene * scene();
    CREATE_FUNC(T04ColorLayer);
    bool init();

};

#endif

.cpp文件

#include "T04ColorLayer.h"

/*CCLayerGradient可以设置颜色的渐变梯度,class CCLayerGradient : public CCLayerColor*/
class LayerGradient :public CCLayerGradient
{
public:
    CREATE_FUNC(LayerGradient);
    bool init()
    {
        /** Initializes the CCLayer with a gradient between start and end in the direction of v.        
        virtual bool initWithColor(const ccColor4B& start, const ccColor4B& end, const CCPoint& v);
        **/
        CCLayerGradient::initWithColor(ccc4(255, 255, 0, 255), ccc4(0, 0, 255, 255), ccp(1, 0));

        return true;
    }

};


CCScene *T04ColorLayer::scene()
{
    CCScene * scene = CCScene::create();

    /*测试背景图层*/
    T04ColorLayer * layer = T04ColorLayer::create();

    /*测试渐变图层*/
    //LayerGradient * layer = LayerGradient::create();

    scene->addChild(layer);
    return scene;
}
bool T04ColorLayer::init()
{
    CCLayerColor::initWithColor(ccc4(255, 0, 255, 255), 200, 200);

    /*设置不忽略锚点,在CCLayer和CCScene中默认是忽略锚点的,*/
    ignoreAnchorPointForPosition(false);

    return true;
}