我正在创建一个包含与我的App游戏相关的数据的文本文件。
我希望在游戏中以每秒显示得分。
如何确保我的int for seconds更新,从零开始
需要输出的示例:
Seconds Score
0 3
1 9
2 16
3 20
.....etc
当前输出(秒始终为0):
Seconds Score
0 3
0 9
0 16
0 20
.....etc
当前代码:
int seconds=0;
//creating header in the txt file Note: Blanked out as it is generating every second
writeToFileEEGPower(order("Seconds")+order("highAlpha")+order("lowAlpha")+order("highBeta")+order("LowBeta")+
order("lowGamma")+order("midGamma")+order("Delta")+order("Theta")+ "\n");
//creating the string to be written to file
String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")+
order(eegPower.lowBeta+"")+order(eegPower.midGamma+"")+order(eegPower.delta+"")+order(eegPower.theta+"")+ "\n";
//write the string to file
writeToFileEEGPower(line);
seconds++;
答案 0 :(得分:1)
我认为你应该为此使用sqlite表。 Greendao 是管理这些表的好工具。您可以每秒将信息保存到表中。游戏结束后,您将获得游戏每一秒的完整分数列表。 在您的代码示例中," second"每次写入新行之前,变量都设置为0。我认为这就是问题。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用处理程序每秒更新一次文本文件:
public class MyActivity extends Activity {
private android.os.Handler mHandler = new android.os.Handler();
private Runnable textFileLogger;
private int seconds = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//Create header for text file
writeToFileEEGPower(order("Seconds") + order("highAlpha") + order("lowAlpha") + order("highBeta") + order("LowBeta") +
order("lowGamma") + order("midGamma") + order("Delta") + order("Theta") + "\n");
textFileLogger = new Runnable() {
@Override
public void run() {
seconds++;
String line = order(seconds + "") + order(eegPower.highAlpha + "") + order(eegPower.lowAlpha + "") + order(eegPower.highBeta + "") +
order(eegPower.lowBeta + "") + order(eegPower.midGamma + "") + order(eegPower.delta + "") + order(eegPower.theta + "") + "\n";
//write the string to file
writeToFileEEGPower(line);
//Repeats the logging after 1 second
mHandler.postDelayed(this, 1000);
}
};
//Starts the logging after 1 second
mHandler.postDelayed(textFileLogger, 1000);
}
@Override
protected void onDestroy() {
super.onDestroy();
//To stop the logging:
mHandler.removeCallbacks(textFileLogger);
}
}