Cocos2D vertically scrollable background

I have three images (320x480) that I'm trying to scroll vertically in a Cocos2D application.

In my initialization method, I have the following:

//adding background sprites background = [CCSprite spriteWithFile:@"BG1.png"]; background2 = [CCSprite spriteWithFile:@"BG2.png"]; //position background sprites background.position = ccp(size.width, size.height/2); background2.position = ccp(size.width, size.height*2); //schedule to move background sprites [self schedule:@selector(scroll:)]; //adding them to the main layer [self addChild:background z:0]; [self addChild:background2 z:0]; 

And here is my scrolling method:

 -(void) scroll:(ccTime)dt { //move 30*dt px vertically background.position = ccp(background.position.x, background.position.y - 30*dt); background2.position = ccp(background2.position.x, background.position.y - 30*dt); //reset offscreen position if (background.position.y < 290) { background.position = ccp(480/2, 480); }else if (background2.position.y < 290) { background2.position = ccp(480/2,480); } } 

Currently, my first background image is offset by about a quarter of the screen (horizontally), and it starts a quarter of the way up from the bottom of the screen, but it scrolls down. My second background image is not really created, the first image just repeats itself again and again at offset. Is there a way to make two images smoothly continuous in the background and how to enable the third image?

Also, just a quick question, is it bad to name objects (I think they are objects) with numbers in their name (for example, background2 / background3)?

+4
source share
1 answer

Tested for horizontal scrolling in landscape mode (all you need to do is change the scroll from horizontal to vertical, you need to understand this), do not forget that ccposition is from the middle of the sprite, and not from 0, 0 perspective ...:

  CGSize size = [CCDirector sharedDirector].winSize; //adding background sprites background = [CCSprite spriteWithFile:@"tracktest.png"]; background2 = [CCSprite spriteWithFile:@"tracktest.png"]; [background.texture setAliasTexParameters]; [background2.texture setAliasTexParameters]; //position background sprites background.position = ccp(background.contentSize.height/2,background.contentSize.width/2); background2.position = ccp(size.width,0); //schedule to move background sprites [self schedule:@selector(scroll:)]; //adding them to the main layer [self addChild:background z:0]; [self addChild:background2 z:0]; 

-scroll method:

 -(void) scroll:(ccTime)dt { //move 30*dt px vertically if (background.position.x<background2.position.x){ background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2); background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2); }else{ background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2); background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2); } //reset offscreen position if (background.position.x <-background.contentSize.width/2) { background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2); }else if (background2.position.x < -background2.contentSize.width/2) { background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2); } } 
+6
source

Source: https://habr.com/ru/post/1345737/


All Articles