如何使用视差在垂直方向上重复背景

时间:2013-04-10 07:40:26

标签: iphone cocos2d-iphone

我一直在寻找垂直方向的背景重复,但总会有一些闪烁..

我在护目镜和一些书上搜索了很多,但没有找到合适的答案 在水平方向上有很多适当的背景重复回复

 -(void) update:(ccTime)delta
  { 
      CCSprite* sprite;
      CCARRAY_FOREACH([spriteBatch children], sprite)
      {

        NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];

        CGPoint pos = sprite.position;
        pos.y -= scrollSpeed * [factor floatValue] * (delta * 50);


        if (pos.y < -screenSize.height)
        {
            pos.y += (screenSize.height * 2) - 2;
        }

         sprite.position = pos;
        }
 }

1 个答案:

答案 0 :(得分:0)

每次加载新背景?我强烈怀疑是否因纹理中的加载时间而导致闪烁。您可以使用线程加载来避免闪烁。

Here is once other question with something similar problem.

Continuous-horizontal-scrolling-of-background-in-cocos2d:请参阅我的回答

连续垂直滚动:

 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 #define MM_BG_SPEED_DUR       ( IS_IPAD ? (6.0f) : (2.0f) )

-(void)onEnter
{
    [super onEnter];
    [self initBackground];

    [self schedule: @selector(tick:)];
}

-(void)initBackground
{
   NSString *tex = @"BG/Background.png";//[self getThemeBG];

    mBG1 = [CCSprite spriteWithFile:tex];
    mBG1.position = ccp(s.width*0.5f,s.height*0.5f);
    [self addChild:mBG1 z:LAYER_BACKGROUND];

    mBG2 = [CCSprite spriteWithFile:tex];
    mBG2.position = ccp(s.width*0.5f,s.height*0.5f+mBG1.contentSize.Height);

    mBG2.flipY = true;
    [self addChild:mBG2 z:LAYER_BACKGROUND];

}


-(void)scrollBackground:(ccTime)dt
{
    CGSize s = [[CCDirector sharedDirector] winSize];

    CGPoint pos1 = mBG1.position;
    CGPoint pos2 = mBG2.position;

    pos1.y -= MM_BG_SPEED_DUR;
    pos2.y -= MM_BG_SPEED_DUR;


    if(pos1.y <=-(s.height*0.5f) )
    {
        pos1.y = pos2.y + mBG2.contentSize.height;
    }

    if(pos2.y <=-(s.height*0.5f) )
    {
          pos2.y = pos1.y + mBG1.contentSize.height;
    }

    mBG1.position = pos1;
    mBG2.position = pos2;

}

-(void)tick:(ccTime)dt
{
    [self scrollBackground:dt];
}