2014-12-04 88 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並檢查它是否平滑滾動。

增加這個,直到你快樂。