Scrolling background - Sprite Kit

前端 未结 3 1306
再見小時候
再見小時候 2020-12-23 18:28

So I tried to create an infinite scrolling background by using this post\'s solution (Sprite kit side scrolling). However, I would want to make the image repeatable. As you

3条回答
  •  -上瘾入骨i
    2020-12-23 19:05

    The original logic that has a for loop works fine with minor changes:

    for (int i = 0; i < 2; i++) {
            SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"bgimage"];
            bg.anchorPoint = CGPointZero;
            bg.position = CGPointMake(i*bg.size.width, 0);
            bg.name = @"snow1";
            [self addChild:bg];
        }
    

    And in the update method:

    [self enumerateChildNodesWithName:@"snow1" usingBlock: ^(SKNode *node, BOOL *stop) {
    SKSpriteNode *bg = (SKSpriteNode *) node;
    
    bg.position = CGPointMake(bg.position.x - 5, bg.position.y);
    
    if (bg.position.x <= -bg.size.width) {
        bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y);
    }
    }];
    

提交回复
热议问题