Here are a couple alternate methods for this.
global Code:
- Code: Select all
int coldir=0;
void changeColor(int rate) {
switch(coldir) {
case 0:
g+=rate;
if (g>=255) {
coldir++;
}
break;
case 1:
r-=rate;
if (r<=0) {
coldir++;
}
break;
case 2:
b+=rate;
if (b>=255) {
coldir++;
}
break;
case 3:
g-=rate;
if (g<=0) {
coldir++;
}
break;
case 4:
r+=rate;
if (r>=255) {
coldir++;
}
break;
case 5:
b-=rate;
if (b<=0) {
coldir=0;
}
break;
}
}
And an even simpler one:
global Code:
- Code: Select all
float colSpeed=4;
int colDir=0;
int colSwitch=0;
int colValues[][]={{1, 0, 0},{0, 0, -1},{0, 1, 0},{-1, 0, 0},{0, 0, 1},{0, -1, 0}};
void rotateColor(){
colSwitch+=colSpeed;
if(colSwitch>=255){
colSwitch=0;
colDir++;
colDir=iLoop(colDir, 0, 5);
}
r+=colValues[colDir][0]*colSpeed;
g+=colValues[colDir][1]*colSpeed;
b+=colValues[colDir][2]*colSpeed;
}
This one is what i use now. It references another function, iLoop() but i always have that function in every game i make anyway. It's a super useful function. Using data arrays to control things is much simpler than if's or switches. In addition, you don't have to test things as much. If you want your enemy to shoot a spread pattern, you can just write the data array right before your spread() function.
viewtopic.php?f=5&t=6854&start=90#p80105Note that you wouldn't use r, g, or b values directly like that. I just changed them to that to make it apparent, because all of this should be in global code. This way, you can run one master color cycle, dump it to three global rgb variables, and have lots of Actors access those colors. You can have some use 255- those values to display the opposite color.
Here's the loop/Constrain functions. Like i said, i have them in every program i write.
- Code: Select all
int iConstrain(int xx, int ll, int ul){
if(xx<ll){xx=ll;}
if(xx>ul){xx=ul;}
return xx;
}
float fConstrain(float xx, float ll, float ul){
if(xx<ll){xx=ll;}
if(xx>ul){xx=ul;}
return xx;
}
int iLoop(int xx, int ll, int ul){
if(xx<ll){xx=ul;}
else if(xx>ul){xx=ll;
}
return xx;
}
float fLoop(float xx, float ll, float ul){
if(xx<ll){xx=ul;}
else if(xx>ul){xx=ll;
}
return xx;
}