Created
February 13, 2014 05:24
-
-
Save kazukitanaka0611/8970189 to your computer and use it in GitHub Desktop.
vertical scroll for SpriteKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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