java - How can I reset my variable? -
how can set variable in 1 class, , change in another, , use changed variable in third class?
i have variable currenttime of application class, meaning can used in activities:
i made game, , whenever person loses, displays time played app:
this works fine, have button on gameover activity. takes user main_menu start game again. thing is, want variable currenttime reset whenever oncreate method of main_menu. otherwise, time keeps on adding onto itself.
for example, if play game 5 seconds , lose, my score five. click "play again!" , play 6 seconds, score should six. instead displays eleven. how stop app adding times, when want reset? need somehow call activitycount.java again, called @ start of app. how address issue? much, appreciate help!
btw: if need more code or think me solve issue, feel free let me know, , try , post here!
here think work, have know idea how...
i'd tempted add constructor activity account class calls application class's constructor, aswell instantiates "currenttime" system.currenttimemillis();
this, or reset variable 0 when request play game again, resetting score. ( maybe make time -= system.currenttimemillis();)
hope helps!
edit:
you try this, second method, creating resettime method
long currenttime = system.currenttimemillis(); public void resettime(){ this.currenttime -= system.currenttimemills(); }
or, first method recommended, this
public activitycount(){ super(); this.currenttime -= system.currenttimemillis(); }
and re-initialise object each time die,
it's kind of hard explain without seeing every class, want reset time each time die, when program first started. done storing initial time ( time when program started ) in seperate variable with-in main class or other class methods. can set currenttime when should need it. 1 of these plans fix's error.
Comments
Post a Comment