设置高分 - SpriteKit

时间:2014-07-05 15:07:03

标签: ios sprite-kit

所以我的得分标签完美无缺。消灭怪物和繁荣,你得到积分。但是,我在实施最佳分数标签方面遇到了一些麻烦。

我知道这是发布的很多代码。但这只是让你可以了解我的游戏究竟在做什么。

以下是我的工作要点:

HudNode.h

#import <SpriteKit/SpriteKit.h>

@interface HudNode : SKNode

@property (nonatomic) NSInteger lives;
@property (nonatomic) NSInteger score;

+ (instancetype) hudAtPosition:(CGPoint)poistion inFrame:(CGRect)frame;

- (void) addPoints:(NSInteger)points;
- (BOOL) loseLife;
@end

HudNode.m

#import "HudNode.h"
#import "Utill.h"

@implementation HudNode

+ (instancetype) hudAtPosition:(CGPoint)poistion inFrame:(CGRect)frame {
HudNode *hud = [self node];
hud.position = poistion;
hud.zPosition = 10;
hud.name = @"HUD";

SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura-CondensedExtraBold"];
scoreLabel.name = @"Score";
scoreLabel.text = @"0";
scoreLabel.fontSize = 24;
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
scoreLabel.position = CGPointMake(frame.size.width-55, -20);
[hud addChild:scoreLabel];


- (void) addPoints:(NSInteger)points {
self.score += points;
SKLabelNode *scoreLabel = (SKLabelNode *)[self childNodeWithName:@"Score"];
scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)self.score];
}

所以这一切都很好,可以显示通过游戏获得的积分。现在,我尝试添加最佳分数的目的如下。

HudMode.h

@property (nonatomic) NSInteger bestScore;

- (void) setHighScore:(NSInteger)highScore; 

然后在HudNode.m中我尝试添加以下内容:

SKLabelNode *bestScoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura-CondensedExtraBold"];
bestScoreLabel.name = @"BestScore";
bestScoreLabel.text = @"0";
bestScoreLabel.fontSize = 24;
bestScoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
bestScoreLabel.position = CGPointMake(frame.size.width-100, -20);
[hud addChild:bestScoreLabel];

- (void) setBestScore:(NSInteger)bestScore {
self.score >= bestScore;
SKLabelNode *bestScoreLabel = (SKLabelNode *)[self childNodeWithName:@"BestScore"];
bestScore.text = [NSString stringWithFormat:@"%ld",()self.bestScore];
}

我甚至得到了这两个标签同时更新的点,但最好的分数根本没有保存。

3 个答案:

答案 0 :(得分:0)

self.score >= bestScore只是一个表达式,并没有为score变量赋值。

我想你会有类似的东西:

if (score > bestScore)
{
    bestScore = score
}

但是,您的代码有点令人困惑。如果您在调用该方法之前已经确定您有一个新的最佳分数,那么您将拥有类似的内容:

bestScore = score;

您还在.h文件中定义了setHighScore方法,但在您的实施中,您有setBestScore

这并不像是一个值得留意的问题/答案,因为它更像是“调试我的代码”。

答案 1 :(得分:0)

您好@MyPalVikram我建议您从NSUserDefaults开始 - &gt;本地存储(在探索如何将分数保存到游戏中心之前)。为了方便你,我提供了用于存储游戏高分的代码。

// Store user profile to NSUserDefaults
- (void)saveUserProfile{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    double bestBossScore = [userDefaults doubleForKey:@"bestBossScore"]; // Get bestHighScore

    collectedStars = self.score;
    [userDefaults setDouble:collectedStars forKey:@"collectedStars"]; // Set collected Stars

    // Compare if the current score is more than the previous high score
    if (self.score > bestBossScore) {
         [userDefaults setDouble:self.score forKey:@"bestBossScore"]; // Set Highscores
    }
    [userDefaults synchronize]; // Remember to include this
}

当游戏结束或用户退出游戏时运行[self saveUserProfile](如果这是你想要的行为)

答案 2 :(得分:-1)

//int = highscore     
//int = _score

 highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] 
    intValue ];

        highScoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 20, 50, 30)];

        highScoreLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:25];

        highScoreLabel.text = [NSString stringWithFormat:@"%ld",(long)highScore];

        highScoreLabel.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:highScoreLabel];

        if (_score > highScore) {
            highScore = _score;
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            highScoreLabel.text = [NSString stringWithFormat:@"%ld", (long)_score];
        }