我已经声明了一个像这样的全局int计数器变量:
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
public int counter = 0;
我正在更新其值onclick:
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
for (Balloon aMBalloonArray : mBalloonArray) {
if (aMBalloonArray.isCollition(x, y)) {
aMBalloonArray.reposition();
popSound.start();
counter = counter + 1;
Log.d("GamePanel", "Score:" + counter);
}
}
return super.onTouchEvent(event);
}
我使用此方法获取值:
public int getCounter() {
return counter;
}
但它显示0而不是更新的值。你能告诉我该怎么做以及如何显示更新的变量。提前谢谢。
调用getcounter():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.final_score);
String score = getIntent().getStringExtra("SCORE");
finalScoreTextView = (TextView)findViewById(R.id.final_score);
gamePanel = new GamePanel(this);
finalScoreTextView.setText(gamePanel.getCounter());
finalScoreTextView.invalidate();
}