移动背景在Spritekit上停止重复

时间:2014-02-23 16:49:04

标签: ios background sprite-kit

这是我的代码,让我的背景重复,只有一个背景,它应该永远重复:

SKTexture* bgTexture = [SKTexture textureWithImageNamed:@"nightbackground"];
bgTexture.filteringMode = SKTextureFilteringNearest;

SKAction* movebgSprite = [SKAction moveByX:-bgTexture.size.width*2 y:0 duration:0.1 * bgTexture.size.width*2];
SKAction* resetbgSprite = [SKAction moveByX:bgTexture.size.width*2 y:0 duration:0];
SKAction* movebgSpritesForever = [SKAction repeatActionForever:[SKAction sequence:@[movebgSprite, resetbgSprite]]];

for( int i = 0; i < 2 + self.frame.size.width / ( bgTexture.size.width * 2 ); ++i ) {
    SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:bgTexture];
    [sprite setScale:1.0];
    sprite.zPosition = -20;
    sprite.position = CGPointMake(self.size.width/2, self.size.height/2);
    [sprite runAction:movebgSpritesForever];
    [self addChild:sprite];

但它不会永远重复,它会在一段时间后停止。可能有什么问题?

1 个答案:

答案 0 :(得分:1)

此案例将以横向方式工作,并使视图重复向左无限滚动(-x方向)。您创建后台SKSPriteNode的两个实例并将它们放在一起。当第一个完全滚动屏幕左侧时,它将被放置在第二个背景精灵节点的右侧。

在didMoveToView中

for (int i = 0; i < 2; i++) {
    SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    bg.anchorPoint = CGPointZero;
    bg.position = CGPointMake(i * bg.size.width, 0);
    bg.name = @"starBackground";
    [self addChild:bg];
}
更新中的

:循环

[self enumerateChildNodesWithName:@"background" usingBlock:^(SKNode *node, BOOL *stop) {

    SKSpriteNode *bg = (SKSpriteNode *)node;
    bg.position = CGPointMake(bg.position.x - 1.5, bg.position.y);

    if (bg.position.x <= -bg.size.width) {
        bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y);
    }

}];