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 can see in the video below, after the image has finished it's horizontal way, there is some empty gap.. I would like to make the image fill that gap, so repeat it endlessly.
http://www.youtube.com/watch?v=kyLTGz7Irrc or https://vimeo.com/79555900 (password: spritekit)
What I did :
for (int i = 0; i < 2; i++) {
SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"bgimage"];
bg.anchorPoint = CGPointZero;
bg.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.origin.y);
bg.name = @"snow1";
[self addChild:bg];
}
and in 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);
}];
Anyway, I fixed it. Just in case someone else will need it, this is how I did it:
then in the update method:
The original logic that has a for loop works fine with minor changes:
And in the update method:
Using Sebyffffd's answer, I've slightly modified the code to display a stack of multiple background images. (7 background images to be exact). And added a little more code for those that are struggling.
in MyScene.h:
in MyScene.m: