【问题标题】:Parallax Scrolling Sprite Kit in Portrait纵向视差滚动精灵套件
【发布时间】:2014-01-21 15:21:27
【问题描述】:

我正在为 iOS 开发一个 SpriteKit 游戏,我正在通过阅读 Ray Wenderlich 教程(Space Shooter SpriteKit)来学习构建游戏

但是我想让应用程序处于纵向模式,因此我想让背景从上到下移动。

    #pragma mark - TBD - Game Backgrounds
    //1
    NSArray *parallaxBackgroundNames = @[@"bg_galaxy.png", @"bg_planetsunrise.png",
                                         @"bg_spacialanomaly.png", @"bg_spacialanomaly2.png"];
    CGSize planetSizes = CGSizeMake(200.0, 200.0);
    //2
    _parallaxNodeBackgrounds = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackgroundNames
                                                                       size:planetSizes
                                                       pointsPerSecondSpeed:10.0];
    //3
    _parallaxNodeBackgrounds.position = CGPointMake(size.width/2.0, size.height/2.0);
    //4
    [_parallaxNodeBackgrounds randomizeNodesPositions];

    //5
    [self addChild:_parallaxNodeBackgrounds];

    //6
    NSArray *parallaxBackground2Names = @[@"bg_front_spacedust.png",@"bg_front_spacedust.png"];
    _parallaxSpaceDust = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackground2Names
                                                                 size:size
                                                 pointsPerSecondSpeed:25.0];
    _parallaxSpaceDust.position = CGPointMake(0,0);
    [self addChild:_parallaxSpaceDust];

编辑:

我可以从上到下移动,但移动不顺畅。

    #pragma mark - TBD - Game Backgrounds
    for (int i = 0; i < 2; i++) {
        SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
        bg.anchorPoint = CGPointZero;
        bg.position = CGPointMake(0, i * bg.size.height);
        bg.name = @"background";
        [self addChild:bg];
    }


    -(void)update:(NSTimeInterval)currentTime {
        [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) {
            SKSpriteNode *bg = (SKSpriteNode *) node;
            bg.position = CGPointMake(bg.position.x, bg.position.y-5);

            if (bg.position.y <= -bg.size.height) {
                bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.height * 2);
            }
        }];
    }

【问题讨论】:

  • 所以用户不会为你编写代码。首先尝试自己实现它,如果遇到困难,请告诉我们您尝试了什么以及您未能理解特定问题或任务的地方。
  • 我成功地从上到下制作了它,但它的移动不如 Ray 教程中的横向模式那么流畅。我会更新代码

标签: ios sprite-kit


【解决方案1】:

我还没有测试过你的代码,但是可能每帧 +5 点太多了,无法顺利移动背景节点? 尝试计算每帧移动的点数:

@implementation GameScene
{
    NSTimeInterval _lastUpdateTime;
    NSTimeInterval _deltaTime;        
}

-(void)update:(NSTimeInterval)currentTime {
    if (_lastUpdateTime) {
        _deltaTime = currentTime - _lastUpdateTime;
    } else {
        _deltaTime = 0;
    }
    _lastUpdateTime = currentTime;

    CGPoint bgVelocity = CGPointMake(0.0, -50.0); // Current speed is 50 points per second
    CGPoint amtToMove = CGPointMake(bgVelocity.x * _deltaTime, bgVelocity.y * _deltaTime);

    [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) {
        SKSpriteNode *bg = (SKSpriteNode *) node;
        bg.position = CGPointMake(bg.position.x+amtToMove.x, bg.position.y+amtToMove.y);

        if (bg.position.y <= -bg.size.height) {
            bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.height * 2);
        }
    }];

【讨论】:

  • 谢谢。顺便说一句,我们怎样才能让滚动背景只在特定时间滚动,比如说 1 或 2 分钟。我们如何在代码中应用它?
  • 这是另一个问题的主题
  • 我一直在尝试创建垂直滚动背景几个小时。我想我没有正确看到它。感谢您的代码。它实际上帮助我纠正了我正在写的内容。 +1
【解决方案2】:

如果您不想动态移动精灵(例如,基于玩家相对于背景的位置),而只想让它们以固定的距离和时间滚动,为什么不做一些真正的事情呢?简单?!

-(void)update:(NSTimeInterval)currentTime {
     [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) {
          [node runAction:[SKAction moveBy:CGVectorMake(0, <distance>) duration:<time>];
     }
}

&lt;distance&gt;&lt;time&gt; 是您将根据您想要的速度/持续时间设置的值。

【讨论】: