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; }?
轉載于:https://www.cnblogs.com/ttss/p/4075660.html
總結
以上是生活随笔為你收集整理的cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat做成服务
- 下一篇: 历史上最伟大的 12 位程序员