Reputation: 1212
Hi i m new to iphone.. Now i am trying to continuous scroll my background. I have added an image as a child . Now the code i m using for continuous scrolling is this on
[bg1 setPosition:ccp(310, 160)];
CCSprite *bg2 = [CCSprite spriteWithFile:@"lines.jpg"];
[bg2 setPosition:ccp(880, 160)];
id enterRight = [CCMoveTo actionWithDuration:4 position:ccp(310, 160)];
id exitLeft = [CCMoveTo actionWithDuration:4 position:ccp(-310, 160)];
id reset = [CCMoveTo actionWithDuration:0 position:ccp(880, 160)];
id enterRight2 = [CCMoveTo actionWithDuration:4 position:ccp(310, 160)];
id exitLeft2 = [CCMoveTo actionWithDuration:4 position:ccp(-310, 160)];
id reset2 = [CCMoveTo actionWithDuration:0 position:ccp(880, 160)];
id seq1 = [CCSequence actions: exitLeft, reset, enterRight, nil];
id seq2 = [CCSequence actions: enterRight2, exitLeft2, reset2, nil];
[bg1 runAction:[CCRepeatForever actionWithAction:seq1]];
[bg2 runAction:[CCRepeatForever actionWithAction:seq2]];
[self addChild:bg1 z:-1 tag:1234];
[self addChild:bg2 z:-1 tag:2345];
The Problem I m getting is this that the image is scrolling continuously alright but when the enter the screen they are adjacent to each other but as they scroll towards the other side of the screen there comes a gap between the two images and it tends to widen as the image scrolls.. Any idea or suggestion why is this happening ? another thing someone said we could use update for background scrolling. Looks something like this.
[self schedule: @selector(update:)];
- (void) update: (ccTime) dt
{
float dy = scroll_speed_per_second * dt;
[self setPosition:ccp(self.position.x,self.position.y + dy)];
}
Just one more thing i wanna ask.. i am able to scroll the images using upper method too but i m unable to reset the image back to there position. Do i have to reset the child back to its position or the whole frame. Any help will be appreciated. Thanks.
Upvotes: 0
Views: 846
Reputation: 1175
heye Nick,
i dont seem to fully understand your code and it doesnt seem to be a good practice too.. what you can do is use CCPlace.
Simply move your sprite till the target position and then use ccplace to reset your sprites position.
Like this...
cloud1=[Obstacle spriteWithFile:@"IT_Cloud1.png" type:CLOUD1];
[self addChild:cloud1 z:4];
cloud1.position=ccp(500,270);
id rptFrvr1=[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:7 position:ccp(-100,270)],
[CCPlace actionWithPosition:ccp(500,270)],nil]];
[cloud1 runAction:rptFrvr1];
cloud2=[Obstacle spriteWithFile:@"IT_Cloud2.png" type:CLOUD2];
[self addChild:cloud2 z:1];
cloud2.position=ccp(700,280);
id rptFrvr2=[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:12 position:ccp(-200,280)],
[CCPlace actionWithPosition:ccp(700,280)],nil]];
[cloud2 runAction:rptFrvr2];
dont get confused by "type" parameter ...dat is just my own implementation of ccsprite..u can simply use ccSprite spritewithfile:
Upvotes: 1