垂直滚动背景 - 精灵套件

时间:2014-04-01 16:43:19

标签: ios objective-c sprite-kit

我正在处理一个处于纵向模式的游戏,并且似乎无法让这个背景在y轴上平滑滚动。它没有正确添加第二个背景,因此两个背景之间总是存在差距。

下面是代码:

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.zRotation = M_PI_2;
        bg.position = CGPointMake(320, self.frame.origin.y);
        bg.anchorPoint = CGPointZero;
        bg.name = @"bg";
        [self addChild:bg];

    }

}

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

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

     }];
}

在两个背景之间存在差距,我做错了什么。注意:如果必要信息,背景大小为568 x 320

感谢。

1 个答案:

答案 0 :(得分:0)

有一些问题。最大的是您的图像是以横向模式制作的。只需在photoshop中编辑图像或预览将其旋转90度,然后取出zRotation。这将适用于你的身高困境。然后更改bg.position = CGPointMake(320,... etc to bg.position = CGPointMake (0, i * bg.size.height);

同时将您的if语句更改为:

         if (bg.position.y <= -bg.size.height)
     {
         bg.position = CGPointMake(0, bg.position.y + bg.size.height*2);
     }

您的代码将有效。