如何在iOS应用中锁定和解锁特定级别?

时间:2014-07-27 22:24:23

标签: objective-c game-development

我正在制作我的第一个游戏并希望锁定我的等级,并在上一级达到某个分数时让它们解锁。我不确定我将如何或在何处实施此功能。这是开始屏幕的代码。感谢您的帮助,如果需要更多信息,请告诉我们!

@implementation StartScreen{
    SKSpriteNode *level2;
    SKSpriteNode *level3;
    SKSpriteNode *level4;
    SKSpriteNode *level5;
    SKSpriteNode *level6;
    SKSpriteNode *level7;
    SKSpriteNode *level8;
    SKSpriteNode *level9;

    SKAction *clicSound;

}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
        [background setSize:self.size];
        [background setPosition:CGPointMake(self.size.width/2, self.size.height/2)];

        [self addChild:background];

        SKSpriteNode *logo = [SKSpriteNode spriteNodeWithImageNamed:@"logo"];
        [logo setSize:CGSizeMake(logo.size.width, logo.size.height)];
        [logo setPosition:CGPointMake(self.size.width/2, self.size.height/2 + 150)];

        [self addChild:logo];

        SKSpriteNode *highscorebutton = [SKSpriteNode spriteNodeWithImageNamed:@"highscorebutton"];
        highscorebutton.name = @"highscorebutton";
        [highscorebutton setPosition:CGPointMake(self.size.width/2, self.size.height/2 - 100)];
        [self addChild:highscorebutton];

        level2 = [SKSpriteNode spriteNodeWithImageNamed:@"level2"];
        level2.name = @"level2";
        [level2 setPosition:CGPointMake(50, self.size.height/2 +10 + 25)];
        [self addChild:level2];

        level3 = [SKSpriteNode spriteNodeWithImageNamed:@"level3"];
        level3.name = @"level3";
        [level3 setPosition:CGPointMake(120, self.size.height/2 +10 +25)];
        [self addChild:level3];

        level4 = [SKSpriteNode spriteNodeWithImageNamed:@"level4"];
        level4.name = @"level4";
        [level4 setPosition:CGPointMake(190, self.size.height/2 +10 +25)];
        [self addChild:level4];

        level5 = [SKSpriteNode spriteNodeWithImageNamed:@"level5"];
        level5.name = @"level5";
        [level5 setPosition:CGPointMake(260, self.size.height/2 +10 +25)];
        [self addChild:level5];

        level6 = [SKSpriteNode spriteNodeWithImageNamed:@"level6"];
        level6.name = @"level6";
        [level6 setPosition:CGPointMake(50, self.size.height/2 -10 -25)];
        [self addChild:level6];

        level7 = [SKSpriteNode spriteNodeWithImageNamed:@"level7"];
        level7.name = @"level7";
        [level7 setPosition:CGPointMake(120, self.size.height/2 -10 -25)];
        [self addChild:level7];

        level8 = [SKSpriteNode spriteNodeWithImageNamed:@"level8"];
        level8.name = @"level8";
        [level8 setPosition:CGPointMake(190, self.size.height/2 -10 -25)];
        [self addChild:level8];

        level9 = [SKSpriteNode spriteNodeWithImageNamed:@"level9"];
        level9.name = @"level9";
        [level9 setPosition:CGPointMake(260, self.size.height/2 -10 -25)];
        [self addChild:level9];

        [[GameCenterManager sharedManager] setDelegate:self];


    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"highscorebutton"]){
        clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
        [self runAction:clicSound];
        BOOL isAvailable = [[GameCenterManager sharedManager] checkGameCenterAvailability];

        if(isAvailable){

            UIViewController *vc = self.view.window.rootViewController;

            [[GameCenterManager sharedManager] presentLeaderboardsOnViewController:vc];

        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"High Score" message: @"Game Center is currently unavailable. Make sure you are logged in." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];

        }
    }
    if ([node.name isEqualToString:@"level2"]){
        clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
        [self runAction:clicSound];
        SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
        [level2 runAction:action completion:^(void){
            [self selectLevel:2];
            SKTransition *transition = [SKTransition fadeWithDuration:.6f];
            GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
            [self.scene.view presentScene:game transition:transition];
        }];
    }
    if ([node.name isEqualToString:@"level3"]){
                clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
                [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level3 runAction:action completion:^(void){
                [self selectLevel:3];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }
    if ([node.name isEqualToString:@"level4"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level4 runAction:action completion:^(void){
                [self selectLevel:4];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }
    if ([node.name isEqualToString:@"level5"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level5 runAction:action completion:^(void){
               [self selectLevel:5];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }
    if ([node.name isEqualToString:@"level6"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level6 runAction:action completion:^(void){
                [self selectLevel:6];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];

    }
    if ([node.name isEqualToString:@"level7"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level7 runAction:action completion:^(void){
                [self selectLevel:7];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }
    if ([node.name isEqualToString:@"level8"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level8 runAction:action completion:^(void){
                [self selectLevel:8];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }
    if ([node.name isEqualToString:@"level9"]) {
            clicSound = [SKAction playSoundFileNamed:@"click.wav" waitForCompletion:YES];
            [self runAction:clicSound];
            SKAction *action = [SKAction rotateByAngle:M_PI duration:.8];
            [level9 runAction:action completion:^(void){
                [self selectLevel:9];
                SKTransition *transition = [SKTransition fadeWithDuration:.6f];
                GameScreen *game = [[GameScreen alloc] initWithSize:self.size];
                [self.scene.view presentScene:game transition:transition];
            }];
    }

}
-(void)selectLevel:(int)level{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:level forKey:@"level"];
    [userDefaults synchronize];
}
-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}
- (void)gameCenterManager:(GameCenterManager *)manager authenticateUser:(UIViewController *)gameCenterLoginController
{

    UIViewController *vc = self.view.window.rootViewController;
    [vc presentViewController:gameCenterLoginController animated:YES completion:^{


    }];
}

2 个答案:

答案 0 :(得分:2)

你应该使用NSUserDefaults并在比赛开始时检查你是否击败了你需要的等级。一旦你达到所需的水平/达到所需的分数,将其保存到NSUserDefaults,然后你的应用将记住锁定和解锁的内容。

答案 1 :(得分:1)

是的,您可以使用NSUserDefaults来处理这种类型的情况。使用此代码可以保存1级的分数。

[[NSUserDefaults standardUserDefaults] setInteger:scoreCount forKey:@"ScoreLevel1"];
[[NSUserDefaults standardUserDefaults] synchronize];

并且对于第2级的限额,请使用此代码

if([[NSUserDefaults standardUserDefaults] integerForKey:@"ScoreLevel1"] >= level1Maximum)
{
      //allow to enter in level 2.
      // put your code here to present level 2 in this condition.
}

您可以使用不同的关键字和限制为所有级别锁定和解锁条件执行此操作,并在达到某个分数限制后刷新级别检查。