This question already has an answer here:
Good afternoon,
For the past few days I've been making a game very similar to DoodleJump in the sense that it involves a main character, things to jump on, certain powerups and some monsters. I've pretty much laid the foundation for everything I need except for one thing. And no, I'm not going to release this game on the app store for publicity, nor do I plan on trying to claiming it as my own game, since the entire game borrows from DoodleJump. I am creating this game as a practice project for myself.
I need some way to implement the "infinite" vertical scrolling that appears in DoodleJump. I've tried several things and searched through many forums and discussion pages, but I can't seem to find one.
Here's some of things I've tried:
- (void) followPlayer
{
static CGSize winSize = [[CCDirector sharedDirector]winSize];
static float fixedPos = winSize.height * .25;
CCLOG(@"Fixed pos: %f",fixedPos);
float newY = fixedPos - mainPlayer.position.y; // mainPlayer
is a custom object I created
newY = MIN(newY,fixedPos);
newY = MAX(newY,-maxHeight - fixedPos); // maxHeight is a float
that keeps track of how high in (y)
coordinates the map is. i.e
every time I add something to the map,
I increment this counter to account
for the new object's height.
CGPoint pos = ccp(self.position.x,newY);
[self setPosition:pos];
}
That method gets called in my update method, so it's supposed to position the screen to follow the character, however it doesn't work as expected. The screen shifts slightly up even when the character hasn't passed the 25% margin that I defined, some static bodies that I created with Box2D also get all messed up.
Is there any other thing I can implement to achieve the "infinite" vertical scroll? I really hit a brick wall on this one, any suggestions would be greatly appreciated.
Edit Here's the code for the failing scrolling background.
This is my BackgroundLayer class that contains the background images:
- (id) init
{
self = [super init];
if(self)
{
back1 = [CCSprite spriteWithFile:@"Background.png"];
back1.anchorPoint = ccp(0,0);
[back1 setPosition:ccp(0,0)];
[self addChild:back1];
[self scheduleUpdate];
}
return self;
}
- (void) update:(ccTime) dt
{
CGPoint b1 = [back1 position];
b1 = [[self parent] convertToNodeSpace:b1];
if(b1.y < 0)
{
back1.position = ccp(0,back1.contentSize.height);
}
}
This class is then being added as a child in my HelloWorldLayer class as the variable layer, which gets scrolled as the player moves up.