2012-05-24 1 views
0

나는 게임을 쓰고있다. 나는 StartScene 클래스가 : StartScene.h에서 :NSThread에서 출구

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

PlayScene, 나는

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

gameMech은 전화 나 PlayScene보기를 호출 할 때의

@interface PlayScene : StartScene { 
} 

그래서 상속 클래스를하다 방법, 모든 주요 게임 물건이 실행됩니다. 일시 중지 버튼과 StartScene 클래스의 해당 메뉴에 대한 모든 버튼이 있습니다. 지금 문제는 재시작 버튼입니다. 그래서 일시 정지 버튼을 쳐서 일시 정지 방법 (즉 StartScene 클래스에),

tView.GameState=GamePaused; 

이이 일시 정지 방식으로 실행 호출 할 때. 게임의 상태가 변경됩니다. 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]; 
} 

이 호출됩니다. 한 가지 문제는 [gameMechThread cancel]; 있다는 것입니다,하지만

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

은 정말 취소 된 것처럼, 그래서 지금은 그것을 주석 작동하지 않습니다. threadStop은 속성이지만 PlayScene 클래스에서는 주 스레드에서 변경되지 않으므로 조건이 제대로 작동하지 않습니다.

이제는 (gameMechThread.isExecuting)의 문제입니다. 그 동안 조건이 호출되어 [NSThread exit];이 실행되기 전에 프로그램이 while 조건에 머무르지 않고 [NSThread exit];이 실행될 때까지 스레드가 이미 중지 된 것처럼 실행됩니다. ([self startGame];)의 새 스레드가 호출되기 전에 스레드 삭제에 도움을 주시겠습니까?

답변

0

NSThread에서 return을 사용하십시오.

if (gameMechThread.isCancelled) { 
     return; 
}