本文介绍了每增加 50 分后如何加快 Sprite Kit 游戏的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a 2D game with Sprite Kit and I need to add a method to speed up the game after every 50 points.

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

I worked out a method that adds the speed as wanted, but I have to copy, paste and adjust an if-statement in the update method for every additional 50 points.

我的解决方案的另一个问题是我必须在加速后立即增加我的积分,因为如果不是,游戏完全加速,虽然我不知道为什么.

Another problem with my solution is that I have to give my points an increment right after speeding it up, because if not the game speeds up completely, though I don't know why.

这是我目前的解决方案:

Here is my solution so far:

// 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++;
}

我想实现此功能,而不必被迫手动完成所有增量,也不想在没有积分增量的情况下加快游戏速度.

I would like to implement this feature without being forced to do all increments manually and without the game speeding up without points increments.

推荐答案

Swift way

您可以使用 didSet 属性观察器根据当前分数更改游戏速度:

You can use didSet property observer to change the game speed based on the current score:

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 在新值存储后立即调用.

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

So, immediately after the new score is set, you should update the gameSpeed variable.

Objective-C 方式

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

In Objective-C there is no such a thing which can provide the exact same behaviour like Swift's didSet and willSet property observers. But there are another ways, for example you can override a score's setter, like this:

#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 的更新逻辑,并在必要时更新速度:

Or, I guess, you can do the same using KVO, but for your example because of simplicity, you can just override a setter. Or maybe you could just make a custom method which will increase the score, handle the gameSpeed's updating logic, and update the speed if necessary:

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

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

So, you will have an additional method definition, which must be called when player scores, in compare to this (assuming that score's setter is overridden):

self.score = someScore;

这完全取决于您.

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

Hope this make sense and it helps!

我已经将 initWithSize: 更改为 initWithCoder,因为从 sks 加载场景时不会调用 initWithSize,这可能是你是怎么做的,因为在 Xcode 7 场景中,默认情况下是从 .sks 文件加载的.

I've changed the initWithSize: to initWithCoder, because initWithSize is not called when the scene is loaded from sks, which is probably how you are doing it, due to fact that in Xcode 7 scene is loaded by default from .sks file.

这篇关于每增加 50 分后如何加快 Sprite Kit 游戏的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:32
查看更多