Reputation: 1042
I have a game and start int value points = 100; and with game progress that value decreases after each click on a button, until the game ends. I subtrack points in onClick method in using switch. After game ends, I reload game method once more for second round and here is my problem. If I make that int variable an instance variable, it will keep it's value after first round ended, and I need to start again from 100. If I declare it as local variable my method that calls another activity using intent and sending final points value will not see this variable. How to solve this?
So, I declare some variables:
int numberPointsGame = 100;
static int numberPointsTotal;
Now, on my onClick method I set some text and subtrack those 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 a user enter correct answer, I place that points in another instance variable and then send it to another popup activity and then reload first activity and do it once more time.
if (ukucanRezultatStrK.equals(konacanNormalized)){
konacnoResenje.setText(mKonacno);
Intent i = 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 the game finish, I go to main menu and set final result (total of two game results) to a button. I ALWAYS GET 0. That's the main problem.
Intent i = new Intent(getApplicationContext(), Izbor.class);
i.putExtra("numberPointsGame", numberPointsTotal);
startActivity(i);
and my 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);
}
Upvotes: 0
Views: 918
Reputation: 122006
Inorder to avoid the creation of instance each time to access the points use Static
class PointsUtil{
public static int points =10
}
in any where in programm
PointsUtil.points= points+new points
When the method reload calls again
PointsUtil.points= 10;
Upvotes: 0
Reputation: 118
while moving from one activity to another, just pass the int along with the intent like
Intent intent = new Intent(...);
intent.putInt("MyImportantInt", someNumber);
and if its just a problem of visibility, try getter and setter ;) or in ur case a methode that just substracts 1 from the int everytime its called
Upvotes: 1
Reputation: 52185
I reload game method once more...
You could have some initialization code which could serve for this purpose... prepare all the necessary fields prior to the game starting:
public class Game
{
private int myCounter;
public void initialize()
{
this.myCounter = 100;
//initialize canvas, connections, etc.
}
}
Or else, simply have a reset()
method in your code which is called prior to the game starting again.
Upvotes: 1