簡體   English   中英

在后台Objective-c中執行游戲中的計分任務

[英]Excute Scoring Tasks in Game in Background objective-c

我正在創建一個游戲,其中在應用程序處於后台或未運行時向用戶獎勵積分。 如果應用程序被完全關閉,他們仍然應該獲得積分。 目前,我正在使用NSTimer進行此操作,但是我讀到了很多計時器無法在后台執行的信息。 這是我所擁有的以及應該如何解決:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(score) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void) score{
    score++;
    [[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score"];
}

您可以在applicationDidEnterBackground方法中使用NSUserDefault設置“結束時間”。 applicationDidBecomeActive您將獲得自“結束時間”以來的經過時間,並根據該經過時間設置得分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM