滚动背景 - 精灵套件

时间:2013-11-16 14:22:49

标签: ios iphone ios7 xcode5 sprite-kit

因此,我尝试使用此帖子的解决方案(Sprite kit side scrolling)创建无限滚动背景。  但是,我想让图像可重复。正如你在下面的视频中所看到的,在图像完成它的水平方式后,有一些空隙......我想让图像填补这个空白,所以无休止地重复它。

http://www.youtube.com/watch?v=kyLTGz7Irrc 要么 https://vimeo.com/79555900(密码:spritekit)

我做了什么:

for (int i = 0; i < 2; i++) {
        SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"bgimage"];
        bg.anchorPoint = CGPointZero;
        bg.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.origin.y);
        bg.name = @"snow1";
        [self addChild:bg];
 }

并在更新方法中:

[self enumerateChildNodesWithName:@"snow1" usingBlock: ^(SKNode *node, BOOL *stop) {
        SKSpriteNode *bg = (SKSpriteNode *) node;
        bg.position = CGPointMake(bg.position.x - 5, bg.position.y);

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

3 个答案:

答案 0 :(得分:22)

无论如何,我修好了。如果其他人需要它,我就是这样做的:

    // Create 2 background sprites
    bg1 = [SKSpriteNode spriteNodeWithImageNamed:@"bg1"];
    bg1.anchorPoint = CGPointZero;
    bg1.position = CGPointMake(0, 0);
    [self addChild:bg1];

    bg2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg2"];
    bg2.anchorPoint = CGPointZero;
    bg2.position = CGPointMake(bg1.size.width-1, 0);
    [self addChild:bg2];

然后在更新方法中:

    bg1.position = CGPointMake(bg1.position.x-4, bg1.position.y);
    bg2.position = CGPointMake(bg2.position.x-4, bg2.position.y);

    if (bg1.position.x < -bg1.size.width)
        bg1.position = CGPointMake(bg2.position.x + bg2.size.width, bg1.position.y);

    if (bg2.position.x < -bg2.size.width) 
        bg2.position = CGPointMake(bg1.position.x + bg1.size.width, bg2.position.y);

答案 1 :(得分:4)

具有for循环的原始逻辑可以很好地进行微小更改:

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

在更新方法中:

[self enumerateChildNodesWithName:@"snow1" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *bg = (SKSpriteNode *) node;

bg.position = CGPointMake(bg.position.x - 5, bg.position.y);

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

答案 2 :(得分:0)

使用Sebyddd的答案,我稍微修改了代码以显示多个背景图像的堆栈。 (准确地说是7个背景图像)。并为那些正在努力的人添加了一些代码。

MyScene.h中的

@interface MyScene : SKScene
{
SKSpriteNode *bg1;
SKSpriteNode *bg2;
SKSpriteNode *bg3;
SKSpriteNode *bg4;
SKSpriteNode *bg5;
SKSpriteNode *bg6;
SKSpriteNode *bg7;
}

在MyScene.m中:

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
// create 7 background sprites
    bg1 = [SKSpriteNode spriteNodeWithImageNamed:@"background1"];
    bg1.anchorPoint = CGPointZero;
    bg1.position = CGPointMake(0, 0);
    [self addChild:bg1];

    bg2 = [SKSpriteNode spriteNodeWithImageNamed:@"background2"];
    bg2.anchorPoint = CGPointZero;
    bg2.position = CGPointMake(bg1.size.width-1, 0);
    [self addChild:bg2];

    bg3 = [SKSpriteNode spriteNodeWithImageNamed:@"background3"];
    bg3.anchorPoint = CGPointZero;
    bg3.position = CGPointMake(bg2.size.width-1, 0);
    [self addChild:bg3];

    bg4 = [SKSpriteNode spriteNodeWithImageNamed:@"background4"];
    bg4.anchorPoint = CGPointZero;
    bg4.position = CGPointMake(bg3.size.width-1, 0);
    [self addChild:bg4];

    bg5 = [SKSpriteNode spriteNodeWithImageNamed:@"background5"];
    bg5.anchorPoint = CGPointZero;
    bg5.position = CGPointMake(bg4.size.width-1, 0);
    [self addChild:bg5];

    bg6 = [SKSpriteNode spriteNodeWithImageNamed:@"background6"];
    bg6.anchorPoint = CGPointZero;
    bg6.position = CGPointMake(bg5.size.width-1, 0);
    [self addChild:bg6];

    bg7 = [SKSpriteNode spriteNodeWithImageNamed:@"background7"];
    bg7.anchorPoint = CGPointZero;
    bg7.position = CGPointMake(bg6.size.width-1, 0);
    [self addChild:bg7];
}
}

- (void)update:(NSTimeInterval)currentTime {
[self moveBackground];
}

-(void)moveBackground
{   
bg1.position = CGPointMake(bg1.position.x-4, bg1.position.y);
bg2.position = CGPointMake(bg2.position.x-4, bg2.position.y);
bg3.position = CGPointMake(bg3.position.x-4, bg3.position.y);
bg4.position = CGPointMake(bg4.position.x-4, bg4.position.y);
bg5.position = CGPointMake(bg5.position.x-4, bg5.position.y);
bg6.position = CGPointMake(bg6.position.x-4, bg6.position.y);
bg7.position = CGPointMake(bg7.position.x-4, bg7.position.y);

if (bg1.position.x < -bg1.size.width){
    bg1.position = CGPointMake(bg7.position.x + bg7.size.width, bg1.position.y);
}

if (bg2.position.x < -bg2.size.width) {
    bg2.position = CGPointMake(bg1.position.x + bg1.size.width, bg2.position.y);
}

if (bg3.position.x < -bg3.size.width) {
    bg3.position = CGPointMake(bg2.position.x + bg2.size.width, bg3.position.y);
}

if (bg4.position.x < -bg4.size.width) {
    bg4.position = CGPointMake(bg3.position.x + bg3.size.width, bg4.position.y);
}

if (bg5.position.x < -bg5.size.width) {
    bg5.position = CGPointMake(bg4.position.x + bg4.size.width, bg5.position.y);
}

if (bg6.position.x < -bg6.size.width) {
    bg6.position = CGPointMake(bg5.position.x + bg5.size.width, bg6.position.y);
}

if (bg7.position.x < -bg7.size.width) {
    bg7.position = CGPointMake(bg6.position.x + bg6.size.width, bg7.position.y);
}
}