开始按钮 - > game.class
- > gameview.class
然后在game.class
上再次循环,当玩家完成第一级时,它将调用gameview.class
再次生成第二级。我的问题是我的计时器也会在进入第二级时重新生成并重新启动。如何在这种游戏流程中暂停和恢复计时器?
在我的Game.Class上:
public void onCreate(Bundle savedInstanceState) {
timeCounter = new TimeCounter();
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
this.maze = (Maze) getLastNonConfigurationInstance();
if (this.maze == null) {
this.maze = (Maze) extras.get("maze");
}
gview = new GameView(this);
gview.setTimeCounter(timeCounter);
gview.setMaze(this.maze);
setContentView(gview);
和我的GameView.Class:
protected void onDraw(Canvas canvas) {
if (startTimer) {
timeCounter.start();
invalidate();
int secondss = timeCounter.getTimeSeconds();
String text = String.format("%02d:%02d", secondss / 60,
secondss % 60);
timer.getTextBounds(text, 0, text.length(), textBounds);
canvas.drawText(text, (this.getWidth() - textBounds.right) - 5,
(this.getHeight() - textBounds.bottom) - 5, timer);
}
当第一级完成时,它将调用此方法:
void shownextmaze() {
Random rand = new Random();
Intent game = new Intent(context, Game.class);
nextmaze = rand.nextInt(6) + 1;
Maze maze = MazeCreator.getMaze(nextmaze);
game.putExtra("maze", maze);
context.startActivity(game);
timeCounter.resume();
}
如何清除4个级别之前我的计时器如何运行?
答案 0 :(得分:1)
合乎逻辑的是,将调用onDraw并重新启动计时器。您应该在服务中启动计时器并在后台线程中运行它。每次要访问它时,您都可以绑定到服务。
选中guideline。
在此代码中,每次打开应用程序时,您都会看到更新的值:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this, MyService.class));
doBindService();
((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mBoundService != null) {
((TextView) findViewById(R.id.textView1))
.setText(mBoundService.getValue() + "");
}
}
});
((Button) findViewById(R.id.button2))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mBoundService = null;
stopService(new Intent(MainActivity.this,
MyService.class));
doUnbindService();
}
});
}
private MyService mBoundService;
private boolean mIsBound;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((MyService.LocalBinder) service).getService();
((TextView) findViewById(R.id.textView1)).setText(mBoundService
.getValue() + "");
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
void doBindService() {
bindService(new Intent(MainActivity.this, MyService.class),
mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
一项服务:
public class MyService extends Service {
private final IBinder mBinder = new LocalBinder();
// private TimeCounter timeCounter;
int x = 0;
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onCreate() {
new Thread(new Runnable() {
public void run() {
while (true) {
x++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int getValue() {
return x;
}
}
也许你需要activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Show Updated value" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="28dp"
android:text="Stop Service" />
</RelativeLayout>