I am new to Cocos2d and am trying to scroll the background horizontally based on pressing a button (menu item). In my game the character stays in the center of the screen and does not move, but the background does.
I cannot seem to reference the x position of my background Sprite. Am I using getChildByTag properly? Is this the proper approach to side scrolling backgrounds (just move the layer?) Have looked over the documentation and sample code extensively and any help that you can provide would be very much appreciated.
in my init() file:
// add arrow menus
CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(moveBgBack:)];
CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(moveBgForward:)];
// set up the arrows etc...
// add background
CCSprite *background = [CCSprite spriteWithFile:bgFileName];
[self addChild:background z:-1 tag:kBackground];
--
move background forward when arrow button is pressed:
-(void)moveBgForward:(id)sender {
CCSprite bgTemp = [layer getChildByTag:KBackground]
CGPoint currentPos = [bgTemp position];
bgTemp.position.x += 10; // ??
// set x position bounds so you don't over-scroll and get a blank screen
}