cocos2d无限循环游戏中的差距

时间:2014-07-05 16:39:39

标签: ios objective-c cocos2d-iphone spritebuilder

我正在使用此代码实现无限循环,但每次更改屏幕外图像坐标时,我都会有1-2秒的间隙。它们为什么会出现?怎么解决?我也在使用SpriteBuilder。

#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 个答案:

答案 0 :(得分: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];

tick方法:

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);
}