0

私はゲームを書いています。私は StartScene クラスを持っています: StartScene.h:

@interface StartScene : UIView <UITextFieldDelegate> {
    ...
    PlayScene* tView;
    NSThread* gameMechThread;
}

PlayScene は継承されたクラスです

@interface PlayScene : StartScene {
}

PlayScene ビューを呼び出すと、次のように呼び出します。

- (void)gameMechThread {
    gameMechThread = [[NSThread alloc]initWithTarget:self selector:@selector(gameMech) object:nil];
    [gameMechThread start];
 }

gameMech は、すべての主要なゲーム要素が実行されるメソッドです。StartScene クラスに一時停止ボタンとそのメニューのすべてのボタンがあります。今のところ問題は再起動ボタンにあります。したがって、一時停止ボタンを押して (StartScene クラスにある) 一時停止メソッドを呼び出すと、

tView.GameState=GamePaused;

これはpauseメソッドで実行されます。ゲームの状態が変更されます。PlayScene クラスでは、常に呼び出されている一時停止メソッドを取得しました

-(void)pause {
    while (self.gameState==GamePaused) {
    }
    if (self.gameState == GameRestarted) {
//        self.threadStop = 2;
        [NSThread exit];
    }
//  if (gameMechThread.isCancelled) {
//       [NSThread exit];
//    }
}

そのため、ゲームは一時停止されます。再起動ボタンを押したら

-(void)restart {
    [gameMechThread cancel];
    [tView.player stop];
    tView.gameState=GameRestarted;
    self.gameState=GameRestarted;
    ...
 //   while (tView.threadStop != 2) { }
 //   tView.threadStop = 1;
    while (gameMechThread.isExecuting) { }

    [tView removeFromSuperview];
    [self startGame];
}

呼ばれます。1つの問題は、あるということですが[gameMechThread cancel];

if (gameMechThread.isCancelled) {
       [NSThread exit];
}

本当にキャンセルされたように機能していないので、とりあえずコメントしました。 threadStopはプロパティですが、PlayScene クラスではメインスレッド上ではなく変更されるため、その条件も正しく機能しません。

そして今の問題(gameMechThread.isExecuting)です。while 条件が呼び出される前に実行されますが、そのスレッドが既に停止しているかのよう[NSThread exit];に、プログラムは実行されるまで while 条件にとどまりません。( ) 内の新しいスレッドが呼び出される[NSThread exit];前に、スレッドを削除してください。[self startGame];

4

1 に答える 1

0

returnNSThreadで 使用します。

if (gameMechThread.isCancelled) {
       return;
}
于 2012-05-24T09:58:52.913 に答える