如何在精灵工具包中创建滚动背景

时间:2014-12-04 05:44:25

标签: ios sprite-kit

我正在使用SpriteKit制作iOS游戏。我使用以下代码填充我的背景,以便它无缝滚动。

static const float BG_VELOCITY = 100.0;
static inline CGPoint CGPointAdd(const CGPoint a, const CGPoint b)
{
    return CGPointMake(a.x + b.x, a.y + b.y);
}

static inline CGPoint CGPointMultiplyScalar(const CGPoint a, const CGFloat b)
{
    return CGPointMake(a.x * b, a.y * b);
}



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

我得到滚动功能,但是当两个背景“连接”时,会出现“白色空间延迟”,如下图所示:

IMG http://i60.tinypic.com/2rctqu0.png

IMG http://i61.tinypic.com/33lh2di.png

1 个答案:

答案 0 :(得分:1)

-(void)update:(NSTimeInterval)currentTime {
    if(!_paused){
        if (_lastUpdateTime) {
            _deltaTime = currentTime - _lastUpdateTime;
        } else {
            _deltaTime = 0;
        }
        _lastUpdateTime = currentTime;
        //change this if you want to scroll horizontally or vertically (now horizontally)
        CGPoint bgVelocity = CGPointMake(_pointsPerSecondSpeed,0.0);
        CGPoint amtToMove = CGPointMake(bgVelocity.x * _deltaTime, bgVelocity.y * _deltaTime);

        _moved = CGPointMake(_moved.x + amtToMove.x, _moved.y + amtToMove.y);

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

        }];
    }
}

您必须在滚动代码中计算时间。那样动画很流畅

在.h文件中创建两个@property _lastUpdateTime和_pointsPerSecondSpeed。

使用5初始化_pointsPerSecondSpeed并检查它是否正在平滑滚动。

增加这个直到你开心。