Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created February 13, 2014 05:24
Show Gist options
  • Save kazukitanaka0611/8970189 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/8970189 to your computer and use it in GitHub Desktop.
vertical scroll for SpriteKit
#import "MyScene.h"
@interface MyScene()
@property (nonatomic, strong) SKSpriteNode *currentBackground;
@property (nonatomic, assign) CFTimeInterval lastUpdateTimeInterval;
@end
static NSString* const BACK_GROUND = @"background";
@implementation MyScene
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.currentBackground = [self createBackground];
[self addChild:self.currentBackground];
}
return self;
}
-(void)update:(CFTimeInterval)currentTime
{
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1)
{
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
[self enumerateChildNodesWithName:BACK_GROUND usingBlock:^(SKNode *node, BOOL *stop) {
node.position = CGPointMake(node.position.x, node.position.y- 200 * timeSinceLast);
if (node.position.y < -(node.frame.size.height + 100))
{
[node removeFromParent];
}
}];
if (self.currentBackground.position.y < -(self.size.height - 100))
{
SKSpriteNode *temp = [self createBackground];
temp.position = CGPointMake(0, self.currentBackground.position.y + self.currentBackground.frame.size.height);
[self addChild:temp];
self.currentBackground = temp;
}
}
- (SKSpriteNode *)createBackground
{
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"background.png"];
background.anchorPoint = CGPointMake(0, 0);
background.position = CGPointMake(0, 0);
background.name = BACK_GROUND;
background.zPosition = -1;
return background;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment