java - Incremental Math Trouble -
i've been perplexed how write math small game i'm creating. game own personal journey learning more java , largely based off of monolith, http://monolith.greenpixel.ca/. i've looked @ source code game can't ascertain how developer math.
here code have far.
package damagedealer; import javax.swing.*; import java.awt.*; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class take1 { int dmg = 0; int epoints = 1000; int intunit1 = 0; public static void main(string[] args) { new take1(); } public take1() { javax.swing.swingutilities.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (unsupportedlookandfeelexception ex) { ex.printstacktrace(); } catch (illegalaccessexception ex) { ex.printstacktrace(); } catch (instantiationexception ex) { ex.printstacktrace(); } catch (classnotfoundexception ex) { ex.printstacktrace(); } jframe frame = new jframe("button clicker"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new addcomponentstopane()); frame.setsize(300, 500); frame.setlocationrelativeto(null); frame.setvisible(true); frame.setresizable(false); } }); } protected class addcomponentstopane extends jpanel { /** * */ private static final long serialversionuid = 1l; public addcomponentstopane() { jpanel apppanel = new jpanel(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.fill = gridbagconstraints.horizontal; gbc.anchor = gridbagconstraints.center; gbc.insets = new insets(5, 5, 5, 5); jlabel evopoints = new jlabel(); evopoints.settext("points: " + string.valueof(epoints)); gbc.gridx = 1; gbc.gridy = 0; apppanel.add(evopoints, gbc); jbutton jbtunit1 = new jbutton("unit 1"); gbc.gridx = 0; gbc.gridy = 1; apppanel.add(jbtunit1, gbc); jlabel lblunit1 = new jlabel(); lblunit1.settext(string.valueof(intunit1)); gbc.gridx = 1; gbc.gridy = 1; apppanel.add(lblunit1, gbc); jbutton jbtdamage = new jbutton("damage"); gbc.gridx = 0; gbc.gridy = 10; apppanel.add(jbtdamage, gbc); jlabel damageoutput = new jlabel(); damageoutput.settext(string.valueof(dmg)); gbc.gridx = 1; gbc.gridy = 10; apppanel.add(damageoutput, gbc); add(apppanel, gbc); jbtdamage.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dmg++; damageoutput.settext(string.valueof(dmg)); if ((dmg % 10) == 0) { epoints++; evopoints.settext("points: " + string.valueof(epoints)); } } }); jbtunit1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (epoints >= 10) { intunit1++; epoints = epoints - 10; lblunit1.settext(string.valueof(intunit1)); evopoints.settext("points: " + string.valueof(epoints)); } } }); } } }
right now, have 2 buttons, jbtunit1
, jbtdamage
. , respective action listeners @ bottom.
the math have works if dmg
value evenly divisible ten. @ point (when dmg
divisible 10), epoints
value incremented. however, jbtunit1
supposed increment dmg
output additional point each unit. therefore, (dmg % 10) == 0
won't work because surpass values multiple times before reaching exact %10
.
question: how should math? or better yet: better (or preferred) method doing type of math?
thanks help! mikey
try replacing this:
dmg++; damageoutput.settext(string.valueof(dmg)); if ((dmg % 10) == 0) { epoints++; evopoints.settext("points: " + string.valueof(epoints)); }
with this:
dmg+= 1+intunit1; //deal more damage more units damageoutput.settext(string.valueof(dmg)); int newepoints = dmg/10 - spentepoints; if(epoints != newepoints) { evopoints.settext("points: " + string.valueof(newepoints)); } epoints = newepoints;
then add spentepoints
field in main class keep track of amount spent , instead of substracting epoints counter every time buy units, add spent amount spentepoints
counter.
note games these tend escalate numbers , integer type variables overflow. take biginteger class lets have almost infinitely large numbers.
Comments
Post a Comment