java - How to declare int variable to be visible to all my methods? -


i have game , start int value points = 100; , game progress value decreases after each click on button, until game ends. subtrack points in onclick method in using switch. after game ends, reload game method once more second round , here problem. if make int variable instance variable, keep it's value after first round ended, , need start again 100. if declare local variable method calls activity using intent , sending final points value not see variable. how solve this?

so, declare variables:

int numberpointsgame = 100; static int numberpointstotal; 

now, on onclick method set text , subtrack points:

public void onclick(view v) {          switch(v.getid()){          case r.id.ba1:             numberpointsgame = numberpointsgame - 5;             a1.settext(ma1);             break;         case r.id.ba2:             numberpointsgame = numberpointsgame - 5;             a2.settext(ma2);             break; 

and now, if user enter correct answer, place points in instance variable , send popup activity , reload first activity , once more time.

if (ukucanrezultatstrk.equals(konacannormalized)){                                                         konacnoresenje.settext(mkonacno);                                                           intent = new intent(asocijacije.this, popup_asocijacije.class);                                                         i.putextra("brojpoenaprimljeno", numberpointsgame);                                                         startactivity(i);                                                          mhandler.postdelayed(mlaunchtask,3700);                                                          numberpointstotal = numberpointstotal + numberpointsgame;                                                          setresults();                                                    }else{                                                     toast.maketext(asocijacije.this, "wrong answer!", toast.length_short).show();                                                     } 

and after second reload, when game finish, go main menu , set final result (total of 2 game results) button. 0. that's main problem.

intent = new intent(getapplicationcontext(), izbor.class);                 i.putextra("numberpointsgame", numberpointstotal);                 startactivity(i); 

and main menu class:

int numberpointsgame;     int score = 0;  @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);               setcontentview(r.layout.izbor);          addlisteneronbutton();          bundle extras = getintent().getextras();          if(extras !=null) {            score = extras.getint("ukupnoasocijacije", 0);         }     }  private void addlisteneronbutton() {  poeniaso.settext("" + score);  } 

i reload game method once more...

you have initialization code serve purpose... prepare necessary fields prior game starting:

public class game {     private int mycounter;      public void initialize()     {          this.mycounter = 100;          //initialize canvas, connections, etc.     } } 

or else, have reset() method in code called prior game starting again.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -