精灵套件:在移动过程中以恒定速度进行口吃

时间:2016-10-02 17:20:50

标签: ios objective-c sprite-kit game-physics

我正在开发一款带有精灵套装的简单飞行游戏。一旦太空船达到最大高度和速度,它就会以恒定的速度飞行。我注意到在恒定飞行期间,太空飞船会随机断断续续。我在这里读到了关于这个问题的所有帖子,但没有任何真正有助于100%修复它。

为了测试,我写了一个非常简单的游戏,它只包含一个太空船和一个简单的云(代码如下)。但即使在这个非常简单的游戏中,太空飞船仍然口吃。 LOG表明即使太空船以恒定速度飞行,太空船的运动也是随机的。这就是口吃的原因。

希望有人可以帮我解决这个问题。谢谢你的任何想法。

Sprite kit,Objective c,Xcode 8.0,测试设备:iPhone 6 - iOS 8.3,iPhone 4s - iOS 9.3.5

CPU:最大21%,内存:最大8 MB,FPS:永久60 FPS

这里是我的代码(为简单起见,我将所有代码都放在了场景类中)

FlightScene.h

#import <SpriteKit/SpriteKit.h>

@interface FlightScene : SKScene <SKPhysicsContactDelegate>

@end

FlightScene.m

#import "FlightScene.h"

#define  HERO_FLIGHT_LOG    1
//#define  HERO_DEBUG_OVERLAY 1

static const CGFloat kMaxHeroVelocityY = 100.0f;
static const CGFloat kMaxHeroVelocityX = 200.0f;

@implementation FlightScene
{
    SKNode *_world;
    SKSpriteNode *_hero;
    SKSpriteNode *_cloud;

    CGPoint _heroStartPosition;
    CGSize _cloudSize;
    CGFloat _xAdj;

    BOOL _hasBegun;

    // debug
    CGFloat _oldHeroX;
    CGFloat _oldHeroY;
    int _frame;

}

- (void)didMoveToView:(SKView *)view
{
    // Setup your scene here

    [super didMoveToView:view];

    _hasBegun = NO;        
    _cloudSize = CGSizeMake(120, 80);
    _xAdj = _cloudSize.width;
    _heroStartPosition = CGPointMake(60, self.size.height/2);

    [self addWorld];
    [self addHero];
    [self addCloud];

    // debug
    _frame = 0;
    _oldHeroX = 0;
    _oldHeroY = 0;
}

#pragma mark - hero

- (void)addHero
{
    _hero = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship2"];

    _hero.size = CGSizeMake(80.0f, 70.0f);
    _hero.position = _heroStartPosition;
    _hero.zPosition = 1;

    _hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2.0f];
    _hero.physicsBody.affectedByGravity = NO;
    _hero.physicsBody.dynamic = YES;
    _hero.physicsBody.allowsRotation = NO;
    _hero.physicsBody.mass = 1.0f;
    _hero.physicsBody.linearDamping = 0.0f;
    _hero.physicsBody.friction = 0.0f;

    [_world addChild:_hero];
}

- (void)updateFlying
{
    if(!_hasBegun)
        return;

    CGVector oldVel = _hero.physicsBody.velocity;
    CGVector newVel = oldVel;

    // increase the velocity
    newVel.dx += (kMaxHeroVelocityX - newVel.dx) / 10.0f;
    newVel.dy += (kMaxHeroVelocityY - newVel.dy) / 10.0f;

    // ensure velocity doesn't exceed maximum
    newVel.dx = newVel.dx > kMaxHeroVelocityX ? kMaxHeroVelocityX : newVel.dx;
    newVel.dy = newVel.dy > kMaxHeroVelocityY ? kMaxHeroVelocityY : newVel.dy;

    _hero.physicsBody.velocity = newVel;
}

- (void)limitHeight
{
    const CGFloat maxHeight = self.size.height * 0.8f;
    if(_hero.position.y > maxHeight)
        _hero.position = CGPointMake(_hero.position.x, maxHeight);
}

- (void)updateFlight
{
    // move hero with constant velocity
    [self updateFlying];

    // ensure height doesn't exceed maximum
    [self limitHeight];
}

#pragma mark - game world

- (void)addWorld
{
    _world = [SKNode new];
    [self addChild:_world];
}

- (void)addCloud
{
    _cloud = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:_cloudSize];

    _cloud.anchorPoint = CGPointMake(0, 1); // top left
    _cloud.position = CGPointMake(self.size.width + _cloudSize.width, self.size.height + _cloudSize.height/2);
    _cloud.zPosition = -1;

    [_world addChild:_cloud];
}

#pragma mark - update world

- (void)updateCloud
{
    // reposition the cloud
    if(_world.position.x + _xAdj < -(_cloudSize.width + self.size.width))
    {
        _xAdj += _cloudSize.width + self.size.width;

        CGFloat y = arc4random_uniform(_cloudSize.height - 10);
        _cloud.position = CGPointMake(_xAdj + self.size.width, self.size.height + y);
    }
}

- (void)updateWorld
{
    // move the world
    CGFloat worldX = -(_hero.position.x - _heroStartPosition.x);
    _world.position = CGPointMake(worldX, _world.position.y);

    [self updateCloud];
    [self flightLog];
}

-(void)update:(CFTimeInterval)currentTime
{
    // Called before each frame is rendered

    if(!_hasBegun)
        return;

    _frame++;

    // update hero movement
    [self updateFlight];
}

- (void)didFinishUpdate
{
    if(!_hasBegun)
        return;

    // update world movement
    [self updateWorld];        
}

#pragma mark - touches

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if(_hasBegun)
        return;

    _hasBegun = YES;
    [self updateFlight];
}


#pragma mark - debug

- (void)flightLog
{

#ifdef HERO_FLIGHT_LOG

    CGFloat newHeroX = _hero.position.x - _heroStartPosition.x;;
    CGFloat diffX = newHeroX - _oldHeroX;

    CGFloat newHeroY = _hero.position.y;
    CGFloat diffY = newHeroY - _oldHeroY;

    NSLog(@"oldHeroY:%f, newHeroY:%f, diffY:%f", _oldHeroY, newHeroY, diffY);
    NSLog(@"oldHeroX:%f, newHeroX:%f, diffX:%f\n\n", _oldHeroX, newHeroX, diffX);

    if(diffX > 3.5f)
    {
        //NSLog(@"\t -> frame:%d fast oldHeroY:%f, newHeroY:%f, diffY:%f", _frame, _oldHeroY, newHeroY, diffY);
        NSLog(@"\t -> frame:%d fast oldHeroX:%f, newHeroX:%f, diffX:%f\n\n", _frame, _oldHeroX, newHeroX, diffX);
    }
    else if(diffX < 3.0f)
    {
        //NSLog(@"\t -> frame:%d fast oldHeroY:%f, newHeroY:%f, diffY:%f", _frame, _oldHeroY, newHeroY, diffY);
        NSLog(@"\t -> frame:%d slow oldHeroX:%f, newHeroX:%f, diffX:%f\n\n", _frame, _oldHeroX, newHeroX, diffX);
    }

    _oldHeroX = newHeroX;
    _oldHeroY = newHeroY;

#endif

}

@end

LOG:

.
.
.
// no stuttering .. FPS: 60

2016-10-02 17:27:19.164 TestFlight[11009:1774440] oldHeroY:301.666534, newHeroY:301.666534, diffY:0.000000
2016-10-02 17:27:19.165 TestFlight[11009:1774440] oldHeroX:263.002899, newHeroX:266.335968, diffX:3.333069

2016-10-02 17:27:19.181 TestFlight[11009:1774440] oldHeroY:301.666534, newHeroY:301.666534, diffY:0.000000
2016-10-02 17:27:19.182 TestFlight[11009:1774440] oldHeroX:266.335968, newHeroX:269.669067, diffX:3.333099

// stuttering .. FPS: 60

2016-10-02 17:27:24.584 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:302.500031, diffY:0.833344
2016-10-02 17:27:24.585 TestFlight[11009:1774440] oldHeroX:1346.335083, newHeroX:1351.335083, diffX:5.000000

2016-10-02 17:27:24.585 TestFlight[11009:1774440]    -> frame:413 fast oldHeroX:1346.335083, newHeroX:1351.335083, diffX:5.000000

2016-10-02 17:27:24.600 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:300.833344, diffY:-1.666687
2016-10-02 17:27:24.601 TestFlight[11009:1774440] oldHeroX:1351.335083, newHeroX:1353.001709, diffX:1.666626

2016-10-02 17:27:24.601 TestFlight[11009:1774440]    -> frame:414 slow oldHeroX:1351.335083, newHeroX:1353.001709, diffX:1.666626

2016-10-02 17:27:24.617 TestFlight[11009:1774440] oldHeroY:300.833344, newHeroY:301.666687, diffY:0.833344
2016-10-02 17:27:24.618 TestFlight[11009:1774440] oldHeroX:1353.001709, newHeroX:1356.335083, diffX:3.333374

2016-10-02 17:27:24.634 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.634 TestFlight[11009:1774440] oldHeroX:1356.335083, newHeroX:1359.668457, diffX:3.333374

2016-10-02 17:27:24.650 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.651 TestFlight[11009:1774440] oldHeroX:1359.668457, newHeroX:1363.001831, diffX:3.333374

2016-10-02 17:27:24.667 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:302.500031, diffY:0.833344
2016-10-02 17:27:24.668 TestFlight[11009:1774440] oldHeroX:1363.001831, newHeroX:1368.001831, diffX:5.000000

2016-10-02 17:27:24.668 TestFlight[11009:1774440]    -> frame:418 fast oldHeroX:1363.001831, newHeroX:1368.001831, diffX:5.000000

2016-10-02 17:27:24.684 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:300.833344, diffY:-1.666687
2016-10-02 17:27:24.684 TestFlight[11009:1774440] oldHeroX:1368.001831, newHeroX:1369.668457, diffX:1.666626

2016-10-02 17:27:24.685 TestFlight[11009:1774440]    -> frame:419 slow oldHeroX:1368.001831, newHeroX:1369.668457, diffX:1.666626

2016-10-02 17:27:24.700 TestFlight[11009:1774440] oldHeroY:300.833344, newHeroY:301.666687, diffY:0.833344
2016-10-02 17:27:24.701 TestFlight[11009:1774440] oldHeroX:1369.668457, newHeroX:1373.001831, diffX:3.333374

2016-10-02 17:27:24.717 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:302.500031, diffY:0.833344
2016-10-02 17:27:24.718 TestFlight[11009:1774440] oldHeroX:1373.001831, newHeroX:1378.001831, diffX:5.000000

2016-10-02 17:27:24.718 TestFlight[11009:1774440]    -> frame:421 fast oldHeroX:1373.001831, newHeroX:1378.001831, diffX:5.000000

2016-10-02 17:27:24.734 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:301.666687, diffY:-0.833344
2016-10-02 17:27:24.734 TestFlight[11009:1774440] oldHeroX:1378.001831, newHeroX:1381.335205, diffX:3.333374

// no stuttering  .. FPS: 60

2016-10-02 17:27:24.750 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.751 TestFlight[11009:1774440] oldHeroX:1381.335205, newHeroX:1384.668579, diffX:3.333374

2016-10-02 17:27:24.767 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.768 TestFlight[11009:1774440] oldHeroX:1384.668579, newHeroX:1388.001953, diffX:3.333374

2016-10-02 17:27:24.784 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.784 TestFlight[11009:1774440] oldHeroX:1388.001953, newHeroX:1391.335327, diffX:3.333374

2016-10-02 17:27:24.801 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.801 TestFlight[11009:1774440] oldHeroX:1391.335327, newHeroX:1394.668701, diffX:3.333374

2016-10-02 17:27:24.817 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.818 TestFlight[11009:1774440] oldHeroX:1394.668701, newHeroX:1398.002075, diffX:3.333374

2016-10-02 17:27:24.834 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.834 TestFlight[11009:1774440] oldHeroX:1398.002075, newHeroX:1401.335449, diffX:3.333374

2016-10-02 17:27:24.850 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.851 TestFlight[11009:1774440] oldHeroX:1401.335449, newHeroX:1404.668823, diffX:3.333374

2016-10-02 17:27:24.867 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.868 TestFlight[11009:1774440] oldHeroX:1404.668823, newHeroX:1408.002197, diffX:3.333374

// stuttering  .. FPS: 60

2016-10-02 17:27:24.883 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:300.833344, diffY:-0.833344
2016-10-02 17:27:24.884 TestFlight[11009:1774440] oldHeroX:1408.002197, newHeroX:1409.668823, diffX:1.666626

2016-10-02 17:27:24.885 TestFlight[11009:1774440]    -> frame:431 slow oldHeroX:1408.002197, newHeroX:1409.668823, diffX:1.666626

2016-10-02 17:27:24.901 TestFlight[11009:1774440] oldHeroY:300.833344, newHeroY:302.500031, diffY:1.666687
2016-10-02 17:27:24.902 TestFlight[11009:1774440] oldHeroX:1409.668823, newHeroX:1414.668823, diffX:5.000000

2016-10-02 17:27:24.902 TestFlight[11009:1774440]    -> frame:432 fast oldHeroX:1409.668823, newHeroX:1414.668823, diffX:5.000000

2016-10-02 17:27:24.917 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:300.833344, diffY:-1.666687
2016-10-02 17:27:24.918 TestFlight[11009:1774440] oldHeroX:1414.668823, newHeroX:1416.335449, diffX:1.666626

2016-10-02 17:27:24.918 TestFlight[11009:1774440]    -> frame:433 slow oldHeroX:1414.668823, newHeroX:1416.335449, diffX:1.666626

2016-10-02 17:27:24.934 TestFlight[11009:1774440] oldHeroY:300.833344, newHeroY:302.500031, diffY:1.666687
2016-10-02 17:27:24.935 TestFlight[11009:1774440] oldHeroX:1416.335449, newHeroX:1421.335449, diffX:5.000000

2016-10-02 17:27:24.935 TestFlight[11009:1774440]    -> frame:434 fast oldHeroX:1416.335449, newHeroX:1421.335449, diffX:5.000000

2016-10-02 17:27:24.950 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:301.666687, diffY:-0.833344
2016-10-02 17:27:24.951 TestFlight[11009:1774440] oldHeroX:1421.335449, newHeroX:1424.668823, diffX:3.333374

// no stuttering for a while (17 seconds .. long time) .. FPS: 60

2016-10-02 17:27:24.967 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:24.968 TestFlight[11009:1774440] oldHeroX:1424.668823, newHeroX:1428.002197, diffX:3.333374
.
.
.

2016-10-02 17:27:41.559 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:41.559 TestFlight[11009:1774440] oldHeroX:4742.992188, newHeroX:4746.325684, diffX:3.333496

// stuttering .. FPS: 60 

2016-10-02 17:27:41.575 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:300.833344, diffY:-0.833344
2016-10-02 17:27:41.576 TestFlight[11009:1774440] oldHeroX:4746.325684, newHeroX:4747.992188, diffX:1.666504

2016-10-02 17:27:41.576 TestFlight[11009:1774440]    -> frame:1432 slow oldHeroX:4746.325684, newHeroX:4747.992188, diffX:1.666504

2016-10-02 17:27:41.592 TestFlight[11009:1774440] oldHeroY:300.833344, newHeroY:302.500031, diffY:1.666687
2016-10-02 17:27:41.593 TestFlight[11009:1774440] oldHeroX:4747.992188, newHeroX:4752.992188, diffX:5.000000

2016-10-02 17:27:41.593 TestFlight[11009:1774440]    -> frame:1433 fast oldHeroX:4747.992188, newHeroX:4752.992188, diffX:5.000000

2016-10-02 17:27:41.609 TestFlight[11009:1774440] oldHeroY:302.500031, newHeroY:301.666687, diffY:-0.833344
2016-10-02 17:27:41.609 TestFlight[11009:1774440] oldHeroX:4752.992188, newHeroX:4756.325684, diffX:3.333496

2016-10-02 17:27:41.625 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:41.626 TestFlight[11009:1774440] oldHeroX:4756.325684, newHeroX:4759.659180, diffX:3.333496

2016-10-02 17:27:41.642 TestFlight[11009:1774440] oldHeroY:301.666687, newHeroY:301.666687, diffY:0.000000
2016-10-02 17:27:41.643 TestFlight[11009:1774440] oldHeroX:4759.659180, newHeroX:4762.992676, diffX:3.333496
.
.
.
// and so on ..

编辑:

  • 我将此添加到addHero:方法

    _hero.physicsBody.angularDamping = 0.0f;
    
  • 我更改了touchesBegan:,以便太空飞船在没有起飞的情况下立即以最大速度飞行。

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        if(_hasBegun)
            return;
    
        _hasBegun = YES;
        _hero.physicsBody.velocity = CGVectorMake(kMaxHeroVelocityX, kMaxHeroVelocityY);
    }
    
    • 我在没有updateFlying:方法的情况下运行代码。太空船在没有起飞的情况下立即以最大速度飞行。它以恒定速度飞行,因此不需要更新速度。

    • 我尝试了其他的事情,比如将更新放在didSimulatePhysics:中。打开飞机模式..正如@Confused建议的那样..但没有任何帮助解决这个问题。它仍然不是100%完美,因为@ crashoverride777说。

我会感激任何其他想法。

3 个答案:

答案 0 :(得分:4)

这是因为SpriteKit的默认物理和运动/变换处理没有更正到/通过增量时间。相反,只要帧速率因系统调用或其他中断而下降,它就会停顿。 iOS设备上发生了很多这样的事情。 iOS系统不断检查网络并监控各种背景活动的其他状态,并响应发送给它的信息。

有两种方法可以处理它。

  1. 启用飞行模式,关闭所有后台更新应用,并严格限制所有应用在其设置中执行后台处理;这可以降低操作系统噪音和开销。

  2. 使用增量时间滚动您自己的调整和补偿机制,这样事情就会继续以正确的速度移动到预期的目的地,即使跳过一两帧也是如此。

  3. 如果你不仅限于SpriteKit,或者不需要它的物理,你可以包含另一个物理引擎并使用它。这最终意味着您可以使用SceneKit,Core Animation甚至Metal来进行渲染....或者SpriteKit

    两个最明智的选择是Box2D和Chipmunk2D。

    以下是将Box2D添加到iOS的教程:https://www.raywenderlich.com/2061-liquidfun-tutorial-with-metal-and-swift-part-1

    这是Chipmunk2D的Swift包装器:https://github.com/jakubknejzlik/ChipmunkSwiftWrapper

答案 1 :(得分:1)

我也在与游戏同样的问题。看来SpriteKit并不特别喜欢不断的运动。

您可以尝试的第一件事是使用

applyForce

而不是直接操纵播放器的力度属性。

当我在常规的

中移动玩家时,我似乎获得了最好的结果
override func update(_ currentTime: TimeInterval) { ... }

方法

我在

中更新相机
override func didFinishUpdate() { ... }

在Apples示例游戏DemoBots中,他们使用相机的约束。在DidSimulatePhysics之后,约束会得到更新,所以当您手动执行时,FininUpdate似乎是最佳位置。

另外请记住,通过执行以下操作,您将获得一些性能

1)在测试时断开手机与Mac的连接。只需在插入时通过xCode运行手机,性能就会降低。

2)当您测试应用时,它处于调试模式,而不是处于发布模式,这会降低性能。

要在测试时手动更改此方案(按下按钮旁边的按钮以运行您的应用)。单击编辑方案,然后在Info中将构建配置更改为Release。

请记住,在完成特定测试后,您应该将其更改回来。您希望在测试应用程序时运行调试内容。

3)确保您的绘制调用非常低。你可以通过确保

来做到这一点
ignoresSiblingOrder 
在您的GameViewController中

设置为true,默认情况下应为。

根据它们所在的层,为所有精灵提供不同的zPosition。

e.g

background = 1
sun = 2
clouds = 3
traps, obstacles, objects = 4
player = 5

4)尽可能多地使用物理主体,因为它们使用的性能最低。

5)在更新方法中尽可能少的代码。

6)缓存纹理,尤其是在使用纹理动画时。

7)如果要对它们进行大量实例化,请对某些节点进行池化。

答案 2 :(得分:1)

我会回答我自己的问题。

飞行阶段:

  • 起飞
  • 一旦太空船达到最大高度和速度,它就会飞上去 恒定速度

问题:

  • 太空飞船在恒定飞行期间随机断断续续

在这种情况下的解决方案:

  • 由于在恒定飞行期间发生口吃,因此定义变量(_maxReached)以控制起飞阶段的结束。

  • 太空船达到最大高度和速度后,设置_maxReached = YES

  • 通过更新x位置(updatePosition)来更新常量航班。

更新的代码:

.
.
// new:
static const CGFloat kMaxSpeed = 5.0f;

@implementation FlightScene
{
.
.
.
    // new:
    BOOL _maxReached;
}

// new: rename
//- (void)updateFlying
- (void)takeOff
{
    if(!_hasBegun)
        return;

    CGVector oldVel = _hero.physicsBody.velocity;
    CGVector newVel = oldVel;

    // increase the velocity
    newVel.dx += (kMaxHeroVelocityX - newVel.dx) / 10.0f;
    newVel.dy += (kMaxHeroVelocityY - newVel.dy) / 10.0f;

    // ensure velocity doesn't exceed maximum
    newVel.dx = newVel.dx > kMaxHeroVelocityX ? kMaxHeroVelocityX : newVel.dx;
    newVel.dy = newVel.dy > kMaxHeroVelocityY ? kMaxHeroVelocityY : newVel.dy;

    _hero.physicsBody.velocity = newVel;    
}

- (void)limitHeight
{
    const CGFloat maxHeight = self.size.height * 0.8f;

    // new 
    if(_hero.position.y >= maxHeight)
    {
    if(_hero.physicsBody.velocity.dy == kMaxHeroVelocityY)
        _maxReached = YES;

        if(_hero.position.y > maxHeight)
            _hero.position = CGPointMake(_hero.position.x, maxHeight);
    }
}

// new: move the hero with constant velocity 
- (void)updatePosition
{
    CGFloat newX = _hero.position.x + kMaxSpeed;
    _hero.position = CGPointMake(newX, _hero.position.y);
}

- (void)updateFlight
{    
    if(_maxReached) // new
    {
        [self updatePosition]; // move the hero with constant velocity
    }
    else
    {
        [self takeOff];

        // ensure height doesn't exceed maximum
        [self limitHeight];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if(_hasBegun)
        return;

    _hasBegun = YES;
    _maxReached = NO; // new
    [self updateFlight];
}