CCLayerColor只是用来创建一个有单一颜色的layer,如果想拥有两种颜色的layer的话,那么就需要用CCLayerGradient来创建了。
先看CCLayerColor来创建一个单颜色的layer的例子:
CCLayerColor *layercolor = CCLayerColor::create(ccc4(255, 0, 0, 255), 100, 100); //layercolor->setAnchorPoint(ccp(0.5f, 0.5f)); layercolor->setCascadeColorEnabled(false); layercolor->setPosition(ccp(0, 0)); this->addChild(layercolor, 1);
这个layer是被忽视掉了它的锚点,也就是说,不管你如何设置它的锚点,它的值始终都是ccp(0.5f, 0.5f).
接下来用CCLayerGradient来创建带有两种颜色的layer,例子代码如下:
CCLayerGradient *layergradient = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(0, 255, 0, 255), CCPoint(50, 50)); layergradient->setAnchorPoint(ccp(0.5f, 0.5f)); layergradient->setPosition(ccp(size.width / 2.0f, size.height / 2.0f)); layergradient->changeWidthAndHeight(400, 400); this->addChild(layergradient, 1);
CCLayerGradient可以通过:
/** change width in Points*/ void changeWidth(GLfloat w); /** change height in Points*/ void changeHeight(GLfloat h); /** change width and height in Points @since v0.8 */ void changeWidthAndHeight(GLfloat w ,GLfloat h);
我们也可以同时通过行为动作,来达到项目中所需要的效果,例子代码如下:
CCActionInterval *fadein = CCFadeIn::create(5); CCActionInterval *fadeOut = fadein->reverse(); CCAction *action = CCRepeatForever::create(dynamic_cast<CCActionInterval *>(CCSequence::create(fadein, fadeOut, NULL))); layergradient->runAction(action);