1.多彩的幕布CCLayerColor,CCLayerGradient

2015-01-27 22:39:40 · 作者: · 浏览: 91
??

1.多彩的幕布(CCLayerColor)

ColorLayer.h

#ifndef_COLORLAYER_H_

#define_COLORLAYER_H_

#include"cocos2d.h"

USING_NS_CC;

//多彩的幕布layer,这时候要使用继承CCLayerColor

//默认的CCLayer是透明的

classColorLayer :publicCCLayerColor

{

public:

staticCCScene *scene();

CREATE_FUNC(ColorLayer);

boolinit();

};

#endif

ColorLayer.cpp

#include"ColorLayer.h"

#include"AppMacros.h"

CCScene *ColorLayer::scene()

{

//先创建一个场景

CCScene *scene =CCScene::create();

//再创建一个层

ColorLayer *layer =ColorLayer::create();

//添加一个层

scene->addChild(layer);

returnscene;

}

boolColorLayer::init()

{

//ccc4(255,255,255,255),200,200 表示颜色使用白色,宽度200,长度200

CCLayerColor::initWithColor(ccc4(255,255,255,255),200,200);

//表示不忽略锚点

ignoreAnchorPointForPosition(false);

returntrue;

}

运行结果:

\

2渐变的Layer

LayerGradient.h

#ifndef_LAYERGRADIENT_H_

#define_LAYERGRADIENT_H_

#include"cocos2d.h"

USING_NS_CC;

//通过这个类实现有色Layer的渐变效果

classLayerGradient :publicCCLayerGradient

{

public:

staticCCScene *scene();

CREATE_FUNC(LayerGradient);

boolinit();

};

#endif

LayerGradient.cpp

#include"LayerGradient.h"

CCScene *LayerGradient::scene()

{

//创建一个场景

CCScene *scene =CCScene::create();

//创建一个layer

LayerGradient *layer =LayerGradient::create();

//场景中添加层

scene->addChild(layer);

returnscene;

}

boolLayerGradient::init()

{

//ccc4(255,0,0,255),ccc4(0,0,255,255),ccp(0,1) 前两个ccc4分别表示两种颜色

//cpp(0,1)表示的是颜色的渐变方向

CCLayerGradient::initWithColor(ccc4(255,150,0,255),ccc4(0,150,15,255),ccp(4,1));

returntrue;

}

运行效果:

\

3 API介绍

CCLayerColor

//将Layer设置成统一的颜色

bool CCLayerColor::initWithColor(const ccColor4B& color);

//通过后面两个参数可以设置设置颜色的Layer的宽度和高度

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

CCLayerGradient

//设置两个渐变颜色

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

//设置两个渐变颜色,并通过最后的const CCPoint设置渐变的方向

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

??