Skip to content

Instantly share code, notes, and snippets.

@YusukeKishino
Created April 15, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YusukeKishino/826a8570f11678c0cb12 to your computer and use it in GitHub Desktop.
Save YusukeKishino/826a8570f11678c0cb12 to your computer and use it in GitHub Desktop.
#include "DynamicGradientLayer.h"
bool DynamicGradientLayer::init()
{
if (!LayerGradient::init()) {
return false;
}
/// init data ////////////////////////////////
min = 185;
max = 242;
startDuration = 13.0f;
endDuration = 11.0f;
setStartColor(Color3B(max,min,min));
setEndColor(Color3B(min,max,min));
setVector(Vec2(0.3,1));
/////////////////////////////////////////////
//updateを毎フレーム呼び出す
scheduleUpdate();
return true;
}
void DynamicGradientLayer::update(float delta)
{
timeForStart += delta;
timeForEnd += delta;
//色が変わるのにかかる時間を超えたらステータスを変える
if (timeForStart > startDuration) {
timeForStart = 0;
startStatus++;
if (startStatus > YtoR) {
startStatus = RtoP;
}
}
if (timeForEnd > endDuration) {
timeForEnd = 0;
endStatus++;
if (endStatus > YtoR) {
endStatus = RtoP;
}
}
//色の変化の差分
auto dStart = (max - min) * timeForStart / startDuration;
auto dEnd = (max - min) * timeForEnd / endDuration;
setStartColor(changeColor(startStatus, dStart));
setEndColor(changeColor(endStatus, dEnd));
}
//状態と差分を渡すと、色を返す
Color3B DynamicGradientLayer::changeColor(int status, int d)
{
switch (status) {
case RtoP:
return Color3B(max, min, min + d);
break;
case PtoB:
return Color3B(max - d,min,max);
break;
case BtoA:
return Color3B(min, min + d, max);
break;
case AtoG:
return Color3B(min, max, max - d);
break;
case GtoY:
return Color3B(min + d, max, min);
break;
case YtoR:
return Color3B(max, max - d, min);
break;
default:
return Color3B(0,0,0);
break;
}
}
#ifndef __TestProject__DynamicGradientLayer__
#define __TestProject__DynamicGradientLayer__
#include "cocos2d.h"
USING_NS_CC;
class DynamicGradientLayer: public LayerGradient
{
private:
//経過時間を保持 StartColor用
float timeForStart = 0.0f;
//経過時間を保持 EndColor用
float timeForEnd = 0.0f;
//RGBの最小値
CC_SYNTHESIZE(int, min, Min);
//RGBの最大値
CC_SYNTHESIZE(int, max, Max);
//startColorの色が変わるのにかかる時間
CC_SYNTHESIZE(float, startDuration, StartDuration);
//endColorの色が変わるのにかかる時間
CC_SYNTHESIZE(float, endDuration, EndDuration);
//色が何色から何色に変わっていくかのステータス
enum kColorStatus {
RtoP = 1, //Red -> Purple
PtoB, //PurPle -> Blue
BtoA, //Blue -> Aqua
AtoG, //Aqua -> Green
GtoY, //Green -> Yellow
YtoR //Yellow -> Red
};
//startColorのステータスを保持
int startStatus = RtoP;
//endColorのステータスを保持
int endStatus = GtoY;
virtual void update(float delta);
Color3B changeColor(int status, int d);
public:
virtual bool init();
CREATE_FUNC(DynamicGradientLayer);
};
#endif /* defined(__TestProject__DynamicGradientLayer__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment