I wrote the code to stop, but why is ONLY the last condition satisfied?

for(Point p : level.brickCoords) { if(new Rectangle((int) (newX), (int) (newY), SPRITE_SCALE + 14, SPRITE_SCALE + 14).intersects(new Rectangle(px, py, level.SCALE,level.SCALE)) && rotate == rotate.NORTH) { speed = 0; if(rotate == rotate.EAST || rotate == rotate.WEST || rotate == rotate.SOUTH) { speed = 2.1f; } }else if(new Rectangle((int) (newX), (int) (newY),SPRITE_SCALE + 14, SPRITE_SCALE + 14).intersects(new Rectangle(px, py, level.SCALE,level.SCALE)) && rotate == rotate.SOUTH) { speed = 0; if(rotate == rotate.EAST || rotate == rotate.WEST || rotate == rotate.NORTH) { speed = 2.1f; } }else if(new Rectangle((int) (newX), (int) (newY),SPRITE_SCALE + 14, SPRITE_SCALE + 14).intersects(new Rectangle(px, py, level.SCALE,level.SCALE)) && rotate == rotate.EAST) { speed = 0; if(rotate == rotate.EAST || rotate == rotate.NORTH || rotate == rotate.SOUTH) { speed = 2.1f; } }else if(new Rectangle((int) (newX), (int) (newY),SPRITE_SCALE + 14, SPRITE_SCALE + 14).intersects(new Rectangle(px, py, level.SCALE,level.SCALE)) && rotate == rotate.WEST) { speed = 0; } if(rotate == rotate.NORTH || rotate == rotate.EAST || rotate == rotate.SOUTH) { speed = 2.1f; } 
  • Tell me, do you know the word refactoring? You have a mountain of duplicate code. Get rid of it to begin with, you see, everything will work ... - Dmitry
  • Tell me, "specialist", what does the mountain mean in your understanding? all that repeats is 'new Rectangle ((int) (newX), (int) (newY), SPRITE_SCALE + 14, SPRITE_SCALE + 14) .intersects (new Rectangle (px, py, level.SCALE, level.SCALE)))' of course, you can do so if (new Rectangle ((int) (newX), (int) (newY), SPRITE_SCALE + 14, SPRITE_SCALE + 14) .intersects (new Rectangle (px, py, level.SCALE, level.SCALE )) && rotate == rotate.NORTH, etc.) ', but it seems to me, with all the droplets, that it will stick to the cube, please understand the code and then just write, refactoring will not help here (yes, it is not needed) ) - My Name - noname
  • Well it is impossible, so it is impossible ... But you still see what polymorphism is. If, in your opinion, 6 conditional operators, 4 of which are nested, placed in a cycle, do not require refactoring, then write further in the same spirit. On the other hand, you are right. This code does not require refactoring. He is so bad that refactoring can not be. You just need to delete it and write again. You can find a mistake here, the question is what will you do when the complexity of the task increases hundreds of times (and in actual development it does) - Dmitriy

0