2014-09-21 3 views
0

bouton de démarrage ->game.class ->gameview.class puis boucle à nouveau sur game.class lorsque le joueur termine le 1er niveau, il appellera gameview.class pour générer à nouveau le 2e niveau. Mon problème est que ma minuterie va également se régénérer et redémarrer lors du passage au 2ème niveau. Comment puis-je faire une pause de ma minuterie et reprendre dans ce genre de jeu?Minuterie onDraw

sur mon 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); 

et sur mon 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); 
    } 

et quand la 1ère arrivée de niveau, il appellera cette méthode:

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(); 

} 

Comment puis-je faire fonctionner ma minuterie jusqu'à ce que les 4 niveaux soient effacés?

Répondre

1

Il est logique que onDraw soit appelé et la minuterie redémarre. Vous devriez démarrer votre minuteur dans un service et l'exécuter dans le fil d'arrière-plan. Chaque fois que vous voulez accéder à cette valeur, vous pouvez lier au service.

Cochez cette case guideline.

Dans ce code chaque fois que vous ouvrez l'application, vous verrez la valeur mise à jour:

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(); 
    } 
} 

Et un service:

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; 
    } 
} 

Peut-être que vous avez besoin 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

Comment puis je faire ça? Pouvez-vous partager quelques exemples de codes sur ce monsieur @Misagh? – icecream

+0

Vous voulez dire que vous n'appelez que la méthode onDraw pour changer de niveau? Donc la méthode onCreate est appelée une fois? –

+0

après la fin du 1er niveau, la méthode shownextmaze dans GameView Class appellera Game.Class puis Game.class appellera GameView.Class où la méthode onDraw a été dessinée. – icecream