首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在每增加50分后加快雪碧套装游戏的速度

如何在每增加50分后加快雪碧套装游戏的速度
EN

Stack Overflow用户
提问于 2016-01-17 15:38:13
回答 1查看 791关注 0票数 2

我正在制作一个2D游戏与雪碧套件,我需要添加一个方法,以加快游戏后,每50分。

我想出了一种按需要增加速度的方法,但我必须复制、粘贴和调整更新方法中的if语句,使其每增加50个点。

我的解决方案的另一个问题是,我必须在加速后给我的积分加分,因为如果没有,游戏就会完全加速,尽管我不知道为什么。

到目前为止,我的解决方案如下:

代码语言:javascript
复制
// 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++;
}

我想要实现这个功能,而不需要手动完成所有的增量,也不需要游戏加速而不增加点数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-17 17:04:10

Swift way

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

代码语言:javascript
复制
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变量。

目标-C路

在Objective中,不存在像Swift的didSet和willSet属性观察者那样提供完全相同行为的东西。但是还有另外一种方法,例如,您可以重写一个得分的设置器,如下所示:

代码语言:javascript
复制
#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。或者,您可以创建一个自定义方法,它将提高得分,处理游戏速度的更新逻辑,并在必要时更新速度:

代码语言:javascript
复制
//Whenever the player scores, you will do this
[self updateScore: score];

因此,您将有一个额外的方法定义,与此相比,必须在牌手得分时调用它(假设score的设置器被覆盖):

代码语言:javascript
复制
self.score = someScore; 

这真的取决于你。

希望这是有意义的,它有帮助!

编辑:

我已经将initWithSize:更改为initWithCoder,因为当场景从sks加载时没有调用initWithSize,这可能就是您这样做的方式,因为在Xcode 7场景中,默认情况下从.sks文件加载场景。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34840157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档