• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows Keeping values moving between two bounds

Erayd

Member
This is code which adds to an alpha value to a certain point and then goes in the other direction. Creating a flash. I'm going over some old code and putting some more thought in to it and my brain is telling me that there are better ways of doing this. How have you done something similarly? Is there a built in method combination that makes this a whole lot better? I think there is.
Code:
if(taking_Damage){
    if(goingDown) current_Alpha -= 0.02;
    if(goingUp) current_Alpha += 0.02;
    if(current_Alpha == 0.99) {
        goingDown = true;
        goingUp = false;
    }
    if(current_Alpha == 0.81){
        goingDown = false;
        goingUp = true;
    }
}
This is how I have refactored it for now:
Code:
taking_damage_a += adding ? 0.02 : -0.02;
  
if(taking_damage_a >= 0.69 || taking_damage_a <= 0.51) adding = !adding;
 
Last edited:

Miradur

Member
Hi, maybe so:

Code:
damage = .02

if (goingUp && current_Alpha < .99) {
    current_Alpha += damage
}

if (goingDown && current_Alpha > .81) {
    current_Alpha += damage * -1
}
With multiplication -1 you can reverse the sign, maybe this will help you find a better solution ;)

Miradur
 
Top