문제

I've had some luck learning how to scroll the background infinitely in Sprite-kit but the current way I've been doing this only scrolls to the left. I have a left and right movement button and would also need the method below to infinitely scroll to the right as well as the left. Ive tried to implement it myself but the results are horrible, any help is appreciated.

How iv'e been scrolling

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

and in update method for when the "right button" is pushed:

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

This method only scrolls to the left, I also need the background to infinitely scroll to the right when the "left button" is pushed.

도움이 되었습니까?

해결책

You are decrementing bg.position.x when scroll to the left. Obviously, you need to increment bg.position.x if you want to move the node to the right:

// if left button is pressed
[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);
    }
}];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top