# GMS 2If Statements Breaking Eachother (SOLVED)

P

#### predikit

##### Guest
I am attempting to work through the code of making the Breakout Game before I actually open the tutorial and I've begun to notice an odd behavior.

If statements are interfering with each other, and I can't for the life of me understand why. I've gotten all the rebound directions to work except one, but the condition was definitely programmed in the collision check. I got so frustrated and decided to restart from the beginning and test specifically for each condition as I worked through the logic.

As an example, take this snippet:

Code:
``````var angle = 0;

//mirrors right rebound

if (direction < 90 ) {

angle = (90 - direction);
direction = (direction + (angle * 2));

}

//mirrors  left rebound

if ((direction > 90) && (direction < 180)) {

angle = (direction - 90);
direction = (direction - (angle * 2));

}``````
The first check for < 90 works, until I code the check for between 90 and 180, then the first condition check for collision will fail and the object passes right through. As soon as I comment out the 2nd If statement, the first one begins working again.

It is my understanding that a logical check for conditions are just that, and should NOT have any affect past it's local scope.

Any ideas as to what's going on here?

#### FrostyCat

##### Member
Like a lot of rookies, you have an issue with yin-yang if blocks.
By "yin-yang if blocks", I mean consecutive if blocks in this form:
Code:
``````if (some_condition) {
//A
}
if (the_opposite_condition) {
//B
}``````
There are 3 issues with this habit:
• Many novices do a poor job negating the condition, producing something that isn't an exact opposite.
• Evaluating the duplicated condition is a waste of CPU time.
• Unlike what many novices believe, A and B are NOT mutually exclusive (i.e. it is possible for both A and B to run). If the code in A enables the opposite condition, B would also be forced to run.
If you only want one of several mutually exclusive if blocks to run no matter what, always use else. Never rely on manually negated conditions.
Code:
``````if (direction < 90) {
...
} else if (direction < 180) {
...
}``````

• predikit and chamaeleon

#### chamaeleon

##### Member
I am attempting to work through the code of making the Breakout Game before I actually open the tutorial and I've begun to notice an odd behavior.

If statements are interfering with each other, and I can't for the life of me understand why. I've gotten all the rebound directions to work except one, but the condition was definitely programmed in the collision check. I got so frustrated and decided to restart from the beginning and test specifically for each condition as I worked through the logic.

As an example, take this snippet:

Code:
``````var angle = 0;

//mirrors right rebound

if (direction < 90 ) {

angle = (90 - direction);
direction = (direction + (angle * 2));

}

//mirrors  left rebound

if ((direction > 90) && (direction < 180)) {

angle = (direction - 90);
direction = (direction - (angle * 2));

}``````
The first check for < 90 works, until I code the check for between 90 and 180, then the first condition check for collision will fail and the object passes right through. As soon as I comment out the 2nd If statement, the first one begins working again.

It is my understanding that a logical check for conditions are just that, and should NOT have any affect past it's local scope.

Any ideas as to what's going on here?
Code runs sequentially. The first block is executed, then the second. If you change direction in the first block, the second will use that updated value. You want to start use the else part of an if statement to avoid this.

P