Scrolling background - Sprite Kit

前端 未结 3 1310
再見小時候
再見小時候 2020-12-23 18:28

So I tried to create an infinite scrolling background by using this post\'s solution (Sprite kit side scrolling). However, I would want to make the image repeatable. As you

3条回答
  •  心在旅途
    2020-12-23 18:45

    Anyway, I fixed it. Just in case someone else will need it, this is how I did it:

        // 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];
    

    then in the update method:

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

提交回复
热议问题