2014-07-05 74 views
0

我使用此代碼來實現無限循環,但每當屏幕外圖像座標發生更改時,我都會在1-2秒內得到間隔。他們爲什麼出現?如何解決它?我也使用SpriteBuilder。cocos2d無限循環遊戲中的空白

#import "MainScene.h" 
static const CGFloat scrollSpeed =100.f; 
@implementation MainScene{ 
CCPhysicsNode *_world; 
    CCNode *_oneb; 
    CCNode *_twob; 
     NSArray *_bb; 


} 
- (void)didLoadFromCCB { 
    _bb = @[_oneb, _twob]; 
} 

-(void)update:(CCTime)delta{ 
    _world.position=ccp(_world.position.x - (scrollSpeed * delta), _world.position.y ); // moving world 
    for (CCNode *ground in _bb) { 
     // get the world position of the ground 
     CGPoint groundWorldPosition = [_world convertToWorldSpace:ground.position]; 
     // get the screen position of the ground 
     CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition]; 
     // if the left corner is one complete width off the screen, move it to the right 
     if (groundScreenPosition.x <= (-1 * ground.contentSize.width)) { 
      ground.position = ccp(ground.position.x + 2 * ground.contentSize.width, ground.position.y); 
     } 
    } 
} 

@end 

編輯:我改變-1到-0.5。工作正常!

enter image description here

enter image description here

回答

1

好像你是在iPhone 4英寸模擬器使用小的圖像爲iPhone的3.5英寸。你的背景圖像的分辨率是多少?

編輯:在我的遊戲中,我也有一個無限循環。也許我的代碼可以幫助你?第一個背景精靈應該是1137x640,第二個1136x640。而且你永遠不會再有空隙!希望能幫助到你。

init方法:

backgroundSprite = [CCSprite spriteWithFile:@"background.png"]; 
    backgroundSprite.anchorPoint = ccp(0,0); 
    backgroundSprite.position = ccp(0,0); 
    [self addChild:backgroundSprite z:0]; 

    backgroundSprite2 = [CCSprite spriteWithFile:@"background2.png"]; 
    backgroundSprite2.anchorPoint = ccp(0,0); 
    backgroundSprite2.position = ccp([backgroundSprite boundingBox].size.width,0); 
    [self addChild:backgroundSprite2 z:0]; 

蜱方法:

backgroundSprite.position = ccp(backgroundSprite.position.x-1,backgroundSprite.position.y); 
backgroundSprite2.position = ccp(backgroundSprite2.position.x-1,backgroundSprite2.position.y); 

if (backgroundSprite.position.x<-[backgroundSprite boundingBox].size.width) { 
    backgroundSprite.position = ccp(backgroundSprite2.position.x+[backgroundSprite2 boundingBox].size.width,backgroundSprite.position.y); 
} 

if (backgroundSprite2.position.x<-[backgroundSprite2 boundingBox].size.width) { 
    backgroundSprite2.position = ccp(backgroundSprite.position.x+[backgroundSprite boundingBox].size.width,backgroundSprite2.position.y); 
} 
+0

背景圖像640:1136。使用4英寸模擬器進行測試。我只是縮小了屏幕截圖。 – Frank

+1

檢出編輯。 – flowmachine1

+0

我在發佈編輯答案之前找到了一個解決方案:)。我將「-1」值更改爲「-0.5」。 – Frank