如何使用spritekit使背景连续垂直滚动

时间:2014-08-10 04:29:23

标签: ios ios7 sprite-kit

我有背景设置,它是水平滚动,这是我的代码:

-(void)initalizingScrollingBackground
{
for (int i = 0; i < 2; i++)
{
    SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    bottomScrollerHeight = bg.size.height;
    bg.position = CGPointMake(i * bg.size.width, 0);
    bg.anchorPoint = CGPointZero;
    bg.name = @"background";

    [self addChild:bg];
}

}

以及此代码:

- (void)moveBottomScroller
{
[self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop)
 {
     SKSpriteNode * bg = (SKSpriteNode *) node;
     CGPoint bgVelocity = CGPointMake(-BG_VELOCITY, 0);
     CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity,_dt);
     bg.position = CGPointAdd(bg.position, amtToMove);

     //Checks if bg node is completely scrolled off the screen, if yes then put it at the end of the other node
     if (bg.position.x <= -bg.size.width)
     {
         bg.position = CGPointMake(bg.position.x + bg.size.width*2,
                                   bg.position.y);
     }

     [bg removeFromParent];
     [self addChild:bg];        //Ordering is not possible. so this is a hack
 }];

}

第二部分使背景滚动。没有它,背景仍然是。

此外,如果没有movebottomscroller,我的精灵会出现在背景之上。使用movebottomscroller,他出现在滚动背景后面。是否有任何指令将他带到前线,超过任何其他背景?

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试以下方法,希望对您有用。

@interface GameScene()

@property (nonatomic) NSTimeInterval lastTimeSceneRefreshed;

@end

@implementation GameScene

- (instancetype)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        [self buildBackground];
        [self startScrolling];
    }
    return self;
}

// This method will add 3 background nodes
- (void)buildBackground {
    float centerX = CGRectGetMidX(self.frame);
    SKSpriteNode *firstBackgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    firstBackgroundNode.name = @"background";
    firstBackgroundNode.position = CGPointMake(centerX,
                     firstBackgroundNode.size.height*firstBackgroundNode.anchorPoint.y);
    [self addChild:firstBackgroundNode];
    float previousYPosition = firstBackgroundNode.position.y;
    for (int i = 0; i < 2; i++) {
        SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
        backgroundNode.position = CGPointMake(centerX,
                                             previousYPosition + backgroundNode.frame.size.height);
        previousYPosition = backgroundNode.position.y;
        backgroundNode.name = @"background";
        [self addChild:backgroundNode];
    }
}

- (void)update:(CFTimeInterval)currentTime {        
    // Updating background nodes
    // We don't want to update backgrounds each frame (60 times per second)
    // Once per second is enough. This will reduce CPU usage
    if (currentTime - self.lastTimeSceneRefreshed > 1) {
        [self backgroundNodesRepositioning];
        self.lastTimeSceneRefreshed = currentTime;
    }
}

- (void)backgroundNodesRepositioning {
    [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop)
    {
        SKSpriteNode *backgroundNode = (SKSpriteNode *)node;
        if (backgroundNode.position.y + backgroundNode.size.height < 0) {
            // The node is out of screen, move it up
            backgroundNode.position = CGPointMake(backgroundNode.position.x, backgroundNode.position.y + backgroundNode.size.height * 3);
        }
    }];
}

- (void)startScrolling {
    SKAction *moveAction = [SKAction moveByX:0 y:-200 duration:1];
    [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop)
    {
        [node runAction:[SKAction repeatActionForever:moveAction] withKey:@"movement"];
    }];
}

答案 1 :(得分:0)

我不完全理解你的方法,但我可以回答你的问题,所以我会继续这样做 - 我希望我没有被误解。

SKNode有一个方法insertChild:atIndex:允许你将背景节点放在后面,或者将精灵放在前面。

SKNode Class Reference

相关问题