ios - 如何在每增加 50 点后加速 Sprite Kit 游戏

标签 ios objective-c sprite-kit 2d-games

我正在使用 Sprite Kit 制作 2D 游戏,我需要添加一种方法来在每 50 点后加速游戏。

我想出了一个方法,可以根据需要增加速度,但是我必须在更新方法中每增加 50 点复制、粘贴和调整一个 if 语句。

我的解决方案的另一个问题是我必须在加速后立即增加我的分数,因为如果不是这样游戏就会完全加速,尽管我不知道为什么。

到目前为止,这是我的解决方案:

// inside the update method
// points are added when an enemy is destroyed
// pointSpeed is the float variable that is used to change positions

if (points == 4) {
    pointSpeed++;
    points++;
}
if (points == 8) {
    pointSpeed++;
    points++;
}
if (points == 12) {
    pointSpeed++;
    points++;
}

我想实现此功能,而不必被迫手动执行所有增量,并且游戏不会在没有积分增量的情况下加速。

最佳答案

快捷方式

你可以使用 didSet 属性观察者根据当前分数改变游戏速度:

import SpriteKit

class GameScene: SKScene {

    var score:Int = 0 {

        didSet{

            if score % 10 == 0 {
               gameSpeed += 0.5
            }

            label.text = "Score : \(score), Speed : \(gameSpeed)"
        }

    }

    var gameSpeed:Double = 1.0

    var label:SKLabelNode = SKLabelNode(fontNamed: "ArialMT")

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        label.text = "Score : \(score), Speed : \(gameSpeed)"
        label.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidX(frame))
        label.fontSize = 18

        addChild(label)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        score++
    }

}

来自文档:

didSet is called immediately after the new value is stored.

因此,在设置新分数后,您应该立即更新 gameSpeed 变量。

Objective-C 方式

在 Objective-C 中,没有这样的东西可以提供与 Swift 的 didSet 和 willSet 属性观察器完全相同的行为。但是还有其他方法,例如您可以覆盖分数的 setter ,如下所示:

#import "GameScene.h"

@interface GameScene()

@property(nonatomic, strong) SKLabelNode *label;

@property(nonatomic, assign) NSUInteger score;

@property(nonatomic, assign) double gameSpeed;

@end

@implementation GameScene

-(instancetype)initWithCoder:(NSCoder *)aDecoder{

NSLog(@"Scene's initWithSize: called");

if(self = [super initWithCoder:aDecoder]){

        /*

         Instance variables in Obj-C should be used only while initialization, deallocation or when overriding accessor methods.
         Otherwise, you should use accessor methods (eg. through properties).

        */

        _gameSpeed  = 1;
        _score      = 0;

       NSLog(@"Variables initialized successfully");
    }

    return self;
}

-(void)didMoveToView:(SKView *)view {

    self.label      = [SKLabelNode labelNodeWithFontNamed:@"ArialMT"];
    self.label.fontSize = 18;
    self.label.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    self.label.text = [NSString stringWithFormat:@"Score : %lu, Speed %f", self.score, self.gameSpeed];
    [self addChild:self.label];

}

//Overriding an actual setter, so when score is changed, the game speed will change accordingly
-(void)setScore:(NSUInteger)score{

    /*

     Make sure that you don't do assigment like this, self.score = score, but rather _score = score
     because self.score = somevalue, is an actual setter call - [self setScore:score]; and it will create an infinite recursive call.

     */
    _score          = score;


    if(_score % 10 == 0){
        self.gameSpeed += 0.5;
    }

    // Here, it is safe to to write something like self.score, because you call the getter.

    self.label.text = [NSString stringWithFormat:@"Score : %lu, Speed %f", self.score, self.gameSpeed];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    self.score++;
}

@end

或者,我想,您可以使用 KVO 来做同样的事情,但对于您的示例,由于简单,您可以只覆盖一个 setter 。或者,也许您可​​以创建一个自定义方法来增加分数,处理 gameSpeed 的更新逻辑,并在必要时更新速度:

//Whenever the player scores, you will do this
[self updateScore: score];

因此,与此相比,您将有一个额外的方法定义,当玩家得分时必须调用该方法(假设得分的 setter 被覆盖):

self.score = someScore; 

这真的取决于你。

希望这是有道理的,它有所帮助!

编辑:

我把initWithSize:改成了initWithCoder,因为initWithSize在从sks加载场景的时候没有调用,这大概是你是怎么做的,因为在 Xcode 7 中场景默认是从 .sks 文件加载的。

关于ios - 如何在每增加 50 点后加速 Sprite Kit 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840157/

相关文章:

ios - FPS 下降和游戏变慢 - Sprite-Kit 和 Swift

ios - 我的所有对象都在我的 iOS 模拟器中消失了,这是为什么?

iphone - 我应该如何针对可能的核心数据文件损坏或加载问题进行防御性编码?

ios - 按钮的 UIScrollView - TouchUpInside?

ios - 使用 NSUndoManager 强制保存 NSManagedObject

ios - 添加一个 SKSpriteNode 到 UIView

ios - parse.com 需要来自其他表的图像

iOS,应用商店应用发布

iphone - 我无法在 UITabBar 中重新排列 UITabBarItem

swift ,SpriteKit : Low FPS with a huge for-loop in update method