I get an error saying “Case expressions must be constant expressions”. I couldn’t fix it no matter what. Is the only solution to change xSpeed as well as origxSpeed or is there a way to set origxSpeed to xSpeed without getting the error?
I won’t send the full code because its like 300+ lines long. Here is what I want to do:
float xSpeed=10
final int origxSpeed=int(xSpeed);
//or do I have to do final int origxSpeed=10?
if ((key == '1') && game==true) {
switch(int(abs(xSpeed))) {
case origxSpeed:
xSpeed = xSpeed * 2/3; //for some reason " *= " has trouble with fractions
ySpeed = ySpeed * 2/3; //for some reason " *= " has trouble with fractions
break;
case 2:
xSpeed *= 1; // *= works here
ySpeed *= 1;
break;
}
}
Also, one more question. Why does " *= " have trouble with fractions such as 2/3 and how can I fix it? When I tried to use " *= " with a fraction the value didn’t change at all. It works perfectly fine with regular numbers and decimals. I need to get this done by next week so I need answers ASAP. Thanks!
better here not use switch / case / break
it anyhow not makes the code shorter or more readable…
use a IF and print the THING , what makes the condition for the IF, first.
The rule is: At least 1 of the operands of the operator / needs to be a float or a double; otherwise the result is an int, w/ its fractional part removed.
Out of interest, is forum.processing.org connected with this website? Many of your posts were on that website when I was looking for answers earlier. If they aren’t, you must be having a hard time answering all the questions that come in here and there. Same goes for some other notable people like Chrisir, Quark etc.
Here you are calculating ballx - width subtracting the the result and imgSize from bally THEN assigning the result to bally
Here you are calculating ballx - width then subtracting imgsize THEN subtracting the result from and assigning* (-=) to bally . You have a double negative
They are different operations
The correct statement is bally -= ballx - width + imgSize