Note that there are some explanatory texts on larger screens.

plurals
  1. PODisabling buttons when a certain variable is zero? [solved: Observer Design Pattern]
    text
    copied!<p>i am creating a simple combat game. there are two opposing players whose objective is to eliminate the other; i have created health bars, and the like. to launch an attack against an opponent, the user clicks a certain button "attack". i want this button to be disabled if one player's healthbar has reached zero. </p> <p><a href="http://sstatic.net/stackoverflow/img/Untitled.jpg" rel="nofollow noreferrer">frame http://sstatic.net/stackoverflow/img/Untitled.jpg</a></p> <p>The structure of my program:</p> <p>main class (public class App extends JFrame) //private JButtons, JPanels, etc.</p> <p>public App() //layout of all components and registered listeners</p> <p>main method //frame/s (i have more than one frame in my main method. haha)</p> <p>Listener class/es //if else statements (if (e.getSource == xyz))</p> <p>JPanel class/es that draws health bars</p> <p>----end of structure</p> <p>here's what i've done so far on the panel class(the total source code is an 800-line txt file):</p> <pre><code> class HP2panel extends JPanel { public void damageTake() { if (widthHP &lt;= 0) { damageToMe = 0; } else if (widthRage &gt;= 250) { damageToMe = 175 + (int)(Math.random() * 25); } else if (widthHP &gt; 0 &amp;&amp; widthRage &lt; 250) { damageToMe = Math.round(1 + (int)(Math.random() * 15)); } if (widthHP2 &lt;= 0) { damageToMe = 0; } widthHP2 = widthHP2 - damageToMe; logtxt.append("\nYour HP: " + widthHP2); logtxt.append("\nDamage you've taken: " + damageToMe); repaint(); } public void healdamageTake() { if (widthHP2 &lt;= 0) { Heal = 0; } if (widthHP2 &gt;= 250) { Heal = 0; } else { Heal = (int)(Math.random() * 25); } widthHP2 = widthHP2 + Heal; logtxt.append("\n"); logtxt.append("\nHealed HP: " + Heal); repaint(); } public void restartHP2() { widthHP2 = 250; repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.green); g.fillRect(0, 0, widthHP2, 16); } } </code></pre> <p>HP2panel is the panel class in which the healthbar(fillRect) of player 2 is drawn. widthHP, widthRage are params from other panel class i've made.</p> <p>i want the attack button disabled once widthHP goes zero or less than. i've worked on if statements but it seemed useless.</p> <p>i don't know whether i'm gonna put it on my Listener class or wherever else in my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload