Reputation: 393
I want to add a background to a side scroll game that moves at a different rate to the main game layer. What would be the best way to do this? I may want 2 background layers, each scrolling slower than the other to give a sense of depth when the player moves.
Also, what is the best way to create a layer, and have direct control over it? At the moment a layer is created but I cant access it in other parts of the code. This is how its done by default which is the way I am using:
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Thanks
Upvotes: 1
Views: 809
Reputation: 22042
CCParallaxNode is good for you.
// background layer: another image
CCSprite *background = [CCSprite spriteWithFile:@"background.png"];
background.scale = 1.5f;
background.anchorPoint = ccp(0,0);
// create a void node, a parent node
CCParallaxNode *voidNode = [CCParallaxNode node];
[voidNode addChild:background z:-1 parallaxRatio:ccp(0.4f,0.5f) positionOffset:CGPointZero];
[voidNode addChild:tilemap z:1 parallaxRatio:ccp(2.2f,1.0f) positionOffset:ccp(0,-200)];
[voidNode addChild:cocosImage z:2 parallaxRatio:ccp(3.0f,2.5f) positionOffset:ccp(200,800)];
[self addChild:voidNode];
Refer ParallaxTest in Cocos2D sample.
Upvotes: 1