I noticed I cannot use a break statement inside of an if statement. I'm using Java.
Compiler will complain to the following:
if(true)
{
break; // invalid code
}
This lead me to have habits of hacking the code everything I cannot break out of the if statement.
I would usually set the boolean flag to false right after execution has been done once because I just want the code to execute once.
EDIT:
Example of hacky approach: The code written below is used for when a character has leveled up in-game. The current level gets incremented, the current and max hp gets updated. The current hp gets updated to the updated max level. Upon level up, the level up logo renders upward above the character and disappears once it has reached a certain final point(this will be dictated by some fixed height in the game code).
All the code will appear in the main character update method.
NOTE: levelUp = true triggers everything(all the hp data to be updated and level up Logo effect to happen.)
public void update()
{
if(exp >= 15 && (level < maxLevel))
{
levelUp = true;
exp = 0;
}
if(updateLevel && (level < maxLevel) )
{
level++;
updateLevel = false;
}
// update current hp
if(updateCurrenHpAfterLevelUp)
{
if(life < maxLife)
{
life++;
}
if(life == maxLife)
{
updateCurrenHpAfterLevelUp = false;
}
}
// updates maxHp
if(updateMaxHpAfterLevelUp)
{
if(maxHpCounter < maxHpIncrease)
{
maxLife++;
maxHpCounter++;
}
if(maxHpCounter == maxHpIncrease)
{
maxHpCounter = 0;
updateCurrenHpAfterLevelUp = true;
updateMaxHpAfterLevelUp = false;
}
}
if(levelUp)
{
levelUpLogo.update();
if(levelUpLogo.getHeightIncrease() == levelUpLogo.getMaxHeight())
{
levelUp = false;
updateMaxHpAfterLevelUp = true;
updateLevel = true;
levelUpLogo.setHeightIncrease(0);
}
}
}