2014-12-04 85 views
0

我正在使用SpriteKit制作iOS游戏。我使用下面的代码来填充我的背景,以便无缝地滚动。如何在sprite工具包中制作滚动背景

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

+0

我刚刚发现这个问题只发生在iPhone 6和PLUS上。 FPS在15时使用PLUS,而在6时为30。iPhone 5s/5/4没有上述问题。 – 2014-12-04 05:51:45

回答

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并检查它是否平滑滚动。

增加这个,直到你快乐。