2013-03-26 35 views
0

現在我已經在應用商店中創建了我的遊戲「激光防禦者」及其應用商店,但我有一件事我無法弄清楚,如何存儲高分在遊戲列表中。我有一個標籤,可以計算被摧毀的敵方船隻的數量,但是如何將這些分數的最好值存儲在列表中?這是我的代碼更新enemiesShot標籤:如何在Cocos2d的清單中存儲高分

  • (無效)更新:(ccTime)DT {

    的NSMutableArray * projectilesToDelete = [[NSMutableArray裏的alloc] INIT]; 爲(在_projectiles CCSprite *射彈){ 的CGRect projectileRect = CGRectMake( projectile.position.x - (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2), projectile.contentSize.width, projectile.contentSize.height);

    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init]; 
    for (CCSprite *target in _targets) { 
        CGRect targetRect = CGRectMake(
                target.position.x - (target.contentSize.width/2), 
                target.position.y - (target.contentSize.height/2), 
                target.contentSize.width, 
                target.contentSize.height); 
    
        if (CGRectIntersectsRect(projectileRect, targetRect)) { 
         [targetsToDelete addObject:target]; 
        } 
    } 
    
    for (CCSprite *target in targetsToDelete) { 
        [_targets removeObject:target]; 
        [self removeChild:target cleanup:YES]; 
        _enemiesShot++; 
        [_enemiesShotLabel setString:[NSString stringWithFormat:@"%d",_enemiesShot]]; 
    
    
    
    } 
    
    if (targetsToDelete.count > 0) { 
        [projectilesToDelete addObject:projectile]; 
    } 
    [targetsToDelete release]; 
    

    }

    爲(在projectilesToDelete CCSprite *射彈){ [_projectiles的removeObject:彈丸]; [self removeChild:拋射清理:YES]; } [projectilesToDelete release];

    }

+0

作者:「list」你的意思是「NSUSerDefaults」,或者你只在應用程序運行時才需要? – Suny 2013-03-27 17:24:04

+0

我想將所有最高分存儲在列表中,無論該應用程序是否正在運行。 – 2013-03-27 20:05:26

回答

0

試試這個:

NSUserDefaults的

保存

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

// saving an NSString 
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"]; 

// saving an NSInteger 
[prefs setInteger:42 forKey:@"integerKey"]; 

// saving a Double 
[prefs setDouble:3.1415 forKey:@"doubleKey"]; 

// saving a Float 
[prefs setFloat:1.2345678 forKey:@"floatKey"]; 

// This is suggested to synch prefs, 
[prefs synchronize]; 

檢索

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

// getting an NSString 
NSString *myString = [prefs stringForKey:@"keyToLookupString"]; 


// getting an NSInteger 
NSInteger myInt = [prefs integerForKey:@"integerKey"]; 

// getting an Float 
float myFloat = [prefs floatForKey:@"floatKey"]; 

當應用程序正在運行時,只保留一些NSArray/MutableArray中的所有數據,當應用程序即將進入後臺時,在「applicationDidEnterBackground」內部的「保存」部分。

當應用程序恢復時,從「UserDefaults」取回所有數據(檢索上面的代碼)。將代碼放置在「ApplicationDidEnterForeground」中。 當應用程序重新啓動時,使用「ViewDidLoad」檢索代碼。