2

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

我在护目镜和一些参考书上搜索了很多,但找不到正确的回复有很多正确的回复,用于水平方向的背景重复

 -(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;
        }
 }
4

1 回答 1

0

每次都加载新背景?我强烈怀疑闪烁是由纹理中的加载时间引起的。您可以使用线程加载来避免闪烁。

这是另一个有类似问题的问题。

Continuous-horizo​​ntal-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];
}
于 2013-04-10T07:49:35.090 回答