Cocos2D vertically scrolling background

前端 未结 1 1806
小蘑菇
小蘑菇 2021-02-02 04:12

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

In my initialization method, I have the following:

//addi         


        
相关标签:
1条回答
  • 2021-02-02 05:00

    Tested for horizontal scrolling in landscape mode (all you have to do is change the scrolling from horizontal to vertical, you should be able to figure this out) dont forget that ccposition is from the middle of the sprite, 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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题