ios - 每增加 50 分后如何加速 Sprite Kit 游戏
<p><p>我正在使用 Sprite Kit 制作 2D 游戏,我需要添加一个方法来在每 50 点后加速游戏。 </p>
<p>我想出了一种方法,可以根据需要增加速度,但我必须在更新方法中复制、粘贴和调整每增加 50 个点的 if 语句。</p>
<p>我的解决方案的另一个问题是,我必须在加速后立即增加积分,因为如果不是,游戏会完全加速,尽管我不知道为什么。</p>
<p>到目前为止,这是我的解决方案:</p>
<pre><code>// 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++;
}
</code></pre>
<p>我想实现这个功能,而不是被迫手动进行所有增量,并且游戏在没有积分增量的情况下加速。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>快捷方式</strong></p>
<p>您可以使用 didSet 属性观察者根据当前分数改变游戏速度:</p>
<pre><code>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++
}
}
</code></pre>
<p>来自文档:</p>
<blockquote>
<p>didSet is called immediately after the new value is stored.</p>
</blockquote>
<p>因此,在设置新分数后,您应该立即更新 <code>gameSpeed</code> 变量。</p>
<p><strong>Objective-C 方式</strong></p>
<p>在 Objective-C 中,没有这样的东西可以提供与 Swift 的 didSet 和 willSet 属性观察器完全相同的行为。但是还有其他方法,例如您可以覆盖分数的 setter ,如下所示:</p>
<pre><code>#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 = ){
/*
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 = ;
self.label.fontSize = 18;
self.label.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.label.text = ;
;
}
//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 - ; 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 = ;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.score++;
}
@end
</code></pre>
<p>或者,我想,您也可以使用 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html" rel="noreferrer noopener nofollow">KVO</a> 来做同样的事情,但是对于您的示例,由于简单性,您可以只覆盖一个 setter 。或者,也许您可以创建一个自定义方法来增加分数,处理 gameSpeed 的更新逻辑,并在必要时更新速度:</p>
<pre><code>//Whenever the player scores, you will do this
;
</code></pre>
<p>因此,与此相比,您将有一个额外的方法定义,必须在玩家得分时调用(假设得分的 setter 被覆盖):</p>
<pre><code>self.score = someScore;
</code></pre>
<p>这完全取决于你。</p>
<p>希望这是有道理的,它会有所帮助! </p>
<p><strong>编辑:</strong></p>
<p>我把<code>initWithSize:</code>改成了<code>initWithCoder</code>,因为从sks加载场景时没有调用<code>initWithSize</code>,这可能是你是怎么做的,因为在 Xcode 7 中,场景默认是从 .sks 文件加载的。</p></p>
<p style="font-size: 20px;">关于ios - 每增加 50 分后如何加速 Sprite Kit 游戏,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34840157/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34840157/
</a>
</p>
页:
[1]