Infinite Scroll View
Infinite background
This is game entry.
We need to use infinite background like Flappy bird.
How to make it?
SKScrollingNode
This can scroll X, or Y.
SKScrollingNode.h
#import <UIKit/UIKit.h> #import <SpriteKit/SpriteKit.h> @interface SKScrollingNode : SKSpriteNode @property (nonatomic)BOOL directionX; // YES : X, NO : Y @property (nonatomic) CGFloat scrollingSpeed; + (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x; - (void) update:(NSTimeInterval)currentTime; @end
SKScrollingNode.m
#import "SKScrollingNode.h" @implementation SKScrollingNode + (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x { UIImage * image = [UIImage imageNamed:name]; SKScrollingNode * node = nil; float image_size = 0.0; if (x) { node = [SKScrollingNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(size, image.size.height)]; image_size = image.size.width; } else { node = [SKScrollingNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(image.size.width, size)]; image_size = image.size.height; } node.scrollingSpeed = 1; node.directionX = x; float total = 0; while(total<(size + image_size)){ SKSpriteNode * child = [SKSpriteNode spriteNodeWithImageNamed:name]; [child setAnchorPoint:CGPointZero]; if(x) { [child setPosition:CGPointMake(total, 0)]; [node addChild:child]; total+=child.size.width; } else { [child setPosition:CGPointMake(0, total)]; [node addChild:child]; total+=child.size.height; } } return node; } - (void) update:(NSTimeInterval)currentTime { if(self.directionX) { [self updateX:currentTime]; } else { [self updateY:currentTime]; } } - (void) updateX:(NSTimeInterval)currentTime { [self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) { child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y); if (child.position.x <= -child.size.width){ float delta = child.position.x+child.size.width; child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y); } }]; } - (void) updateY:(NSTimeInterval)currentTime { [self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) { child.position = CGPointMake(child.position.x, child.position.y - self.scrollingSpeed); if (child.position.y <= -child.size.height){ float delta = child.position.y+child.size.height; child.position = CGPointMake(child.position.x, child.size.height*(self.children.count-1)+delta); } }]; } @end [/cpp] <h3>How to use</h3> <h4>Background.h</h4> This is extended class. [cpp] #import <SpriteKit/SpriteKit.h> #import "SKScrollingNode.h" @interface BackgroundNode : SKScrollingNode @end
Background.m
@implementation BackgroundNode + (id) scrollingNodeWithImageNamed:(NSString *)name inContainerSize:(float) size x:(BOOL)x { SKScrollingNode * node = [super scrollingNodeWithImageNamed:name inContainerSize:size x:x]; node.name = @"background"; return node; } @end
Scene
#import "FirstScene.h" #import "BackgroundNode.h" @interface FirstScene() @property (nonatomic)BackgroundNode *background; @end @implementation FirstScene -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { self.background = [BackgroundNode scrollingNodeWithImageNamed:@"background" inContainerSize:self.frame.size.width x:YES]; [self addChild:self.background]; } return self; } - (void)update:(CFTimeInterval)currentTime { /* Called before each frame is rendered */ [self.background update:currentTime]; } @end