Reputation: 43
Currently i have the following code
if(BlackScreenAlpha > 1f){
BlackScreenAlphaIncrease = false;
}
if(BlackScreenAlphaIncrease == true){
BlackScreenAlpha += 0.01f;
}else{
BlackScreenAlpha -= 0.01f;
}
Which Currently changes the alpha of an image from 0, to 1, back to 0 again. I have encountered this a few times in my project, and am now wondering if there is a more efficient way of doing this.
Is there a better way to correspond the BlackScreenAlphaIncrease bool more directly to how BlackScreenAlpha changes? I.e directly convert 'true' to the '+' sign, and likewise 'false' to the '-' sign?
(I do not believe a while loop is appropriate, as this is only a slither of my code and other stuff runs between each increment/decrement)
Upvotes: 0
Views: 229
Reputation: 38767
You can't really have a "sign", unless you do something like this:
float signMultiplier;
if(BlackScreenAlphaIncrease)
{
signMultiplier = 1;
}
else
{
signMultiplier = -1;
}
BlackScreenAlpha += (signMultiplier * 0.01f);
But this is less readable. What about using the ternary conditional operator instead?:
BlackScreenAlpha += BlackScreenAlphaIncrease ? 0.01f : -0.01f;
Upvotes: 2