SpriteKit - Creating a timer

前端 未结 5 1950
小鲜肉
小鲜肉 2020-11-22 05:15

How can I create a timer that fires every two seconds that will increment the score by one on a HUD I have on my screen? This is the code I have for the HUD:



        
5条回答
  •  我在风中等你
    2020-11-22 05:30

    The following code creates a new thread and waits 2 seconds before doing something on the main thread:

    BOOL continueIncrementingScore = YES;
    
    dispatch_async(dispatch_queue_create("timer", NULL);, ^{
        while(continueIncrementingScore) {
            [NSThread sleepForTimeInterval:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                // this is performed on the main thread - increment score here
    
            });
        }
    });
    

    Whenever you want to stop it - just set continueIncrementingScore to NO

提交回复
热议问题