2013-04-10 38 views
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; 
     } 
} 

回答

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]; 
} 
+0

嗨,日Thnx快速回復... 我最關心的是我想要的背景重複頂部向在垂直方向下... 我試過你的鏈接,但背景重複從左到右 我改變x和y,但它沒有工作 – Drake 2013-04-10 08:15:30

+0

ok ..update d我的答案.. – Guru 2013-04-10 09:12:22

+0

嗨,謝謝你這麼好的回覆.. 一個問題通過..而上下的屏幕顯示空白每次...你可以看到背景空.. – Drake 2013-04-10 09:21:57