At the moment this is what my code looks like:
if (!flipped){
scaleX -= 0.1;
if (scaleX < 0.0001){
sounds.playSound("flick");
flipped = true;
}
}
Here is what I’d like it to look like:
if (!flipped){
scaleX -= 0.1;
if (scaleX == 0){
sounds.playSound("flick");
flipped = true;
}
}
How can I achieve this? Because the double value (scaleX) is never actually 0.
You want to define some sort of tolerance, then do:
You shouldn’t be checking for the value being exactly zero – just “close enough to zero”. That’s just a way of life with binary floating point types… things like “0.1” and “0.0001” aren’t exactly representable in binary floating point, which is why you run into this sort of thing.
It’s not clear why you don’t like your original code, and why you want it to be the second form. Other than not taking the absolute value, the first code looks okay to me. (I’d extract out the constant, but that’s a separate matter.)