2014-09-21 60 views
0

开始按钮 - >game.class - >gameview.class然后再次循环game.class当玩家完成第一个级别时,它会调用gameview.class再次生成第二个级别。我的问题是我的计时器也会在第二级时重新生成并重新启动。如何让我的计时器暂停并恢复到这种游戏流程?onDraw上的定时器

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:

我Game.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级被清除?

回答

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> 
+0

我怎样才能做到这一点?你能分享一下这位先生@Misagh的代码吗? – icecream 2014-09-21 04:16:32

+0

你的意思是你只调用onDraw方法来改变级别?那么onCreate方法被调用一次? – 2014-09-21 04:23:48

+0

第一级完成后,GameView类中的shownextmaze方法将调用Game.Class,然后Game.class将调用绘制onDraw方法的GameView.Class。 – icecream 2014-09-21 04:27:53