user1791574
user1791574

Reputation: 1749

background image is not properly scrolling in cocos2d

I write the code for scrolling image in background of game. Simply define two sprite and set their position.

CGSize screenSize=[[CCDirector sharedDirector]winSize];

sky1 = [CCSprite spriteWithFile:@"sky1.png"];

sky1.position=ccp(screenSize.width/2, screenSize.height/2);
[self addChild:sky1];
CCLOG(@"sky1 position  width=%f    height=%f",sky1.position.x,sky1.position.y);
sky2=[CCSprite spriteWithFile:@"sky2.png"];
sky2.position=ccp(screenSize.width/2, sky1.position.y+sky1.contentSize.height/2+sky2.contentSize.height/2);
[self addChild:sky2];
[self schedule:@selector(scroll:) interval:0.01f];

And write the code for scrolling in scroll method:

-(void)scroll:(ccTime)delta{
    CGSize screen=[[CCDirector sharedDirector] winSize];
    CCLOG(@"come from schedule method    pos.y=%f",sky1.position.y);
    sky1.position=ccp(sky1.position.x, sky1.position.y-1);
    sky2.position=ccp(sky2.position.x, sky2.position.y-1);
    if(sky1.position.y <= -([sky1 boundingBox].size.height));
    {
         sky1.position=ccp(sky1.position.x, sky2.position.y+[sky2 boundingBox].size.height);

        CCLOG(@"COMING IN first ifin scroll mwthod  position width=%f  pos.y=%f",sky1.position.x,sky1.position.y);
    }
    if(sky2.position.y<= -[sky2 boundingBox].size.height);
    {
        sky2.position=ccp(sky2.position.x, sky1.position.y+[sky1 boundingBox].size.height);
        CCLOG(@"coming in secnond if ");
    }
}

When I remove the if conditions then it works properly for one time. I don't know what is wrong with my condition and what the hell is going on with my code. Can anybody explain?

Upvotes: 1

Views: 286

Answers (1)

Sylvan
Sylvan

Reputation: 536

I'm not too sure why your code isn't working properly. One thought that pops in my head is your interval may be too short. In other words, 0.01f (1/100) is shorter than your game update speed which is most likely 0.016 (1/60). So first thing I would try is tweaking your interval to maybe 0.02f. You could also drop the interval argument from the schedule call so it just runs once every frame. I would also try dropping the ccTime argument since it isn't being used.

[self schedule:@selector(scroll)];

-(void)scroll {}

But besides all that, this is probably best handled using CCActions. This looks like a simple pair of CCMoveTo actions combined with a CCRepeatForever to get the effect you want. This is the way I would go.

EDIT: here is a bit more detail about using CCActions to accomplish this. There are multiple ways of doing the same thing, but here is one you could try:

float moveDuration = 3; // or however fast you want it to scroll
CGPoint initialPosition = ccp(screenSize.width/2, screenSize.height/2);
CGPoint dp = ccp(0, -1*sky1.contentSize.height); // how far you want it to scroll down
CCMoveBy *scrollSky1 = [CCMoveBy actionWithDuration:moveDuration position:dp];
CCMoveTo *resetSky1 = [CCMoveTo actionWithDuration:0 position:initialPosition];
CCSequence *sequence = [CCSequence actionOne:scrollSky1 two:resetSky1];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:sequence];
[sky1 runAction:repeat];

Upvotes: 2

Related Questions