2013-02-26 89 views
4

我很好奇如何在cocos2d中創建無限背景。例如,讓我說我正在建立一個應用程序,其中一個人從左到右運行,我希望他無限運行。那麼在那種情況下,我將不得不擁有無盡的背景,這樣這個人就可以繼續奔跑。我一直在搜尋這個問題,並沒有發現任何實際的工作。Cocos2D無限背景圖片

任何類型的建議,答案和提示都非常感謝。

由於

+0

閱讀此:http://www.learn-cocos2d.com/2012/12/ways-scrolling-cocos2d-explained/ – LearnCocos2D 2013-02-26 21:05:03

回答

5

試試這個:

#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+s.width*0.5f,s.height*0.5f); 

    mBG2.flipX = 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.x -= MM_BG_SPEED_DUR; 
    pos2.x -= MM_BG_SPEED_DUR; 


    if(pos1.x <=-(s.width*0.5f)) 
    { 
     pos1.x = pos2.x + s.width; 
    } 

    if(pos2.x <=-(s.width*0.5f)) 
    { 
     pos2.x = pos1.x + s.width; 
    } 

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

} 

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

的最簡單的方法是將包括無縫齧合在一起的兩個背景圖像。 (CCSprite可以正常工作)一旦第一個背景完全離開屏幕,您的更新方法就會將其移回第二個背景旁邊的屏幕另一側,並不斷移動兩個背景圖像。爲第二個背景重複此過程。

0

嘗試this。這很容易實施和運作良好。只需按照閱讀我的教程。