0

XCode 5 を使用しています。Cocos2D を使用してゲームを作成しています。また、いくつかの関数を順番に繰り返し実行したり、次々と実行したりします。一度に多くのアクションが実行されるため、遅延も必要です (これにより、アクションが次のアクションに干渉されず、前のアクションの効果をキャンセルしたり、再スケジュールして機能をすぐに実行したりしません)。

インターフェイスがフリーズし、Debug Navigator で CPU 消費が 99 ~ 100% に達し、メモリが徐々に増加し、増加し続けます。すべての例外ブレークポイントも配置します。しかし、例外は発生しません。

私はこのコードを使用しています

-(void)threadMainRunLoop
{
    BOOL exitNow = NO;
    int loopCount = 1;
    [self function1];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

    // Add the exitNow BOOL to the thread dictionary.
    NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
    [threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:@"ThreadShouldExitNow"];

    // Install an input source.
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(function2) userInfo:nil repeats:NO];
    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function3) userInfo:nil repeats:NO];
    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function4) userInfo:nil repeats:NO];
    [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(function5) userInfo:nil repeats:NO];
    [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(function6) userInfo:nil repeats:NO];

    while (loopCount <= 4 && !exitNow)
    {
        // Do one chunk of a larger body of work here.
        // Change the value of the moreWorkToDo Boolean when done.
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(function2) userInfo:nil repeats:NO];
        [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function3) userInfo:nil repeats:NO];
        [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function4) userInfo:nil repeats:NO];
        [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(function5) userInfo:nil repeats:NO];
        [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(function6) userInfo:nil repeats:NO];

        // Run the run loop but timeout immediately if the input source isn't waiting to fire.
        [runLoop runUntilDate:[NSDate dateWithTimeInterval:5.0 sinceDate:[NSDate date]] ];

        // Check to see if an input source handler changed the exitNow value.
        exitNow = [[threadDict valueForKey:@"ThreadShouldExitNow"] boolValue];
        ++loopCount;
    }
}

そして、この関数を で呼び出しますccTouchesEnded:withEvent:

「Listing 2-3 Checking for an exit condition during a long job」のスレッド管理からこのコードをたどりました。

私の NSRunLoop は私のゲーム/インターフェースをフリーズさせますか???

デバイスがハングしていないことを覚えています。function1-6 は UI を更新しています。また、ゲームがフリーズする時期は予期されていません。

前もって感謝します。

4

1 に答える 1

0

cocos2d はスレッドセーフではないため、これらのスケジュールされた関数のいずれかが cocos2d アクションを実行したり、バックグラウンド スレッドでノードを変更したりすると、問題が発生します。代わりに、cocos2d の組み込みの scheduleSelector:interval: メソッドを使用してください。

ここでの特定の問題は、while ループです。メインスレッドを 5 秒間ブロックする runUntilDate を呼び出し、それを最大 4 回実行します。つまり、cocos2d と ui は 20 秒ごとに状態を更新して描画します。アプリがフリーズするのも不思議ではありません。

マルチスレッド化を行いたい場合は、performSelectorInBackground またはグランド セントラル ディスパッチを使用します。アプリの実行ループ全体とすべてのスレッドを制御しないと、実行ループとスレッドのレベルが低すぎます。

于 2013-10-25T10:27:56.573 に答える