-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameBGLayer.m
executable file
·97 lines (73 loc) · 3 KB
/
GameBGLayer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// GameBGLayer.m
// CanaClone
//
// Created by Josh Click on 12/31/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "GameBGLayer.h"
@implementation GameBGLayer
- (void)addScrollingBackgroundWithParallax
{
//1st set
CCSprite *bgLayer = [CCSprite spriteWithSpriteFrameName:@"midground1.png"];
bgLayer.position = ccp(0,screenSize.height/2-50);
[objectSpriteBatchNode addChild:bgLayer z:2];
bgWidth = bgLayer.boundingBox.size.width-1;
CCSprite *mgLayer = [CCSprite spriteWithSpriteFrameName:@"background.png"];
mgLayer.position = ccp(0,screenSize.height/2+50);
[objectSpriteBatchNode addChild:mgLayer z:1];
//2nd set
CCSprite *bgLayer2 = [CCSprite spriteWithSpriteFrameName:@"midground2.png"];
bgLayer2.position = ccp(bgWidth, screenSize.height / 2-50);
[objectSpriteBatchNode addChild:bgLayer2 z:2];
CCSprite *mgLayer2 = [CCSprite spriteWithSpriteFrameName:@"background.png"];
mgLayer2.position = ccp(bgWidth, screenSize.height / 2+50);
[objectSpriteBatchNode addChild:mgLayer2 z:1];
//3nd set
CCSprite *bgLayer3 = [CCSprite spriteWithSpriteFrameName:@"midground1.png"];
bgLayer3.position = ccp(bgWidth*2, screenSize.height / 2-50);
[objectSpriteBatchNode addChild:bgLayer3 z:2];
CCSprite *mgLayer3 = [CCSprite spriteWithSpriteFrameName:@"background.png"];
mgLayer3.position = ccp(bgWidth*2, screenSize.height / 2+50);
[objectSpriteBatchNode addChild:mgLayer3 z:1];
//4th set
CCSprite *bgLayer4 = [CCSprite spriteWithSpriteFrameName:@"midground2.png"];
bgLayer4.position = ccp(bgWidth*3, screenSize.height / 2-50);
[objectSpriteBatchNode addChild:bgLayer4 z:2];
CCSprite *mgLayer4 = [CCSprite spriteWithSpriteFrameName:@"background.png"];
mgLayer4.position = ccp(bgWidth*3, screenSize.height / 2+50);
[objectSpriteBatchNode addChild:mgLayer4 z:1];
// Set background color to light gray.
CCLayerColor* colorLayer = [CCLayerColor layerWithColor:ccc4(176, 176, 191, 255)];
[self addChild:colorLayer z:0];
// Initialize the array that contains the scroll factors for individual stripes.
speedFactors=[NSMutableArray arrayWithCapacity:3];
[speedFactors addObject:[NSNumber numberWithFloat:9.0f]];
[speedFactors addObject:[NSNumber numberWithFloat:0.5f]];
[speedFactors addObject:[NSNumber numberWithFloat:1.0f]];
}
-(void) update:(ccTime)delta withSpeed:(float)scrollSpeed
{
for (CCSprite* sprite in objectSpriteBatchNode.children)
{
NSNumber* factor=[speedFactors objectAtIndex:sprite.zOrder];
CGPoint pos=sprite.position;
pos.x -= scrollSpeed/2 * factor.floatValue * delta;
if (pos.x < -bgWidth) {
pos.x += bgWidth * 4;
}
sprite.position = pos;
}
}
-(id)init {
if (self = [super init]) {
screenSize = [[CCDirector sharedDirector] winSize];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bg_atlas.plist"];
objectSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"bg_atlas.png"];
[self addChild:objectSpriteBatchNode z:10];
[self addScrollingBackgroundWithParallax];
}
return self;
}
@end