代码之家  ›  专栏  ›  技术社区  ›  kmtk

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

  •  2
  • kmtk  · 技术社区  · 10 年前

    我正在进行我的第一场比赛,希望锁定我的等级,并在上一级达到一定分数时解锁。我不确定我将如何或在何处实现这一点。这是开始屏幕上的代码。感谢您的帮助,如果需要更多信息,请告诉我!

    @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 回复  |  直到 6 年前
        1
  •  2
  •   Wyetro    10 年前

    你应该使用 NSUserDefaults 当游戏开始时,检查你是否达到了要求的水平。一旦你达到要求的水平/达到要求的分数,将其保存到 NSUser默认值 然后你的应用程序就会记住锁定和解锁的内容。

        2
  •  1
  •   Max    10 年前

    是的,您可以使用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.
    }
    

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