2014-09-10 86 views
1

在gameScene背景水平移動它工作正常,但我想移動背景緩慢和快速根據分數增加,我用nstimeinterval改變這種方式進行,但沒有我可以看到改變背景移動緩慢和快速其只是正常,如果這種方式是錯的,請別人幫我。改變背景移動速度

SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"background10"]; 

    NSTimeInterval _move; 
     int _gameScore; 
    if (_gameScore>=5) { 
     _move =0.09; 
    }else{ 
     _move = 0.02; 
    } 

    SKAction *movBg = [SKAction moveByX:-backgroundTexture.size.width*2 y:0 duration:_move*backgroundTexture.size.width]; 
    SKAction *resetBg =[SKAction moveByX:backgroundTexture.size.width*2 y:0 duration:0]; 


    SKAction *movebackgroundforever =[SKAction repeatActionForever:[SKAction sequence:@[movBg,resetBg]]]; 
    for (int i= 0; i<2 + self.frame.size.width/(backgroundTexture.size.width*2); ++i) { 
     SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:backgroundTexture]; 
     [sprite setScale:1.0]; 
     sprite.zPosition = -20; 
     sprite.anchorPoint = CGPointZero; 
     sprite.position = CGPointMake(i* sprite.size.width, 0); 
     [sprite runAction:movebackgroundforever]; 
     [_bgLayer addChild:sprite]; 
+0

我認爲你需要將'_gameScore'直接乘以'_move',並且爲什麼要乘以'backgroundTexture.size.width'。 – 2014-09-10 14:33:58

+0

可以如何編輯代碼? – anjani 2014-09-10 14:38:08

+0

,因爲它的時間間隔寬度水平移動 – anjani 2014-09-10 14:46:05

回答

0

試試這個代碼:

將直接乘動畫時間與得分_move*__gameScore,你也可以提供一些intalize價值_move變量。

SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"background10"]; 

NSTimeInterval _move; 


SKAction *movBg = [SKAction moveByX:-backgroundTexture.size.width*2 y:0 duration:_move*__gameScore]; 
SKAction *resetBg =[SKAction moveByX:backgroundTexture.size.width*2 y:0 duration:0]; 


SKAction *movebackgroundforever =[SKAction repeatActionForever:[SKAction sequence:@[movBg,resetBg]]]; 
for (int i= 0; i<2 + self.frame.size.width/(backgroundTexture.size.width*2); ++i) { 
    SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:backgroundTexture]; 
    [sprite setScale:1.0]; 
    sprite.zPosition = -20; 
    sprite.anchorPoint = CGPointZero; 
    sprite.position = CGPointMake(i* sprite.size.width, 0); 
    [sprite runAction:movebackgroundforever]; 
    [_bgLayer addChild:sprite]; 
+0

多數民衆贊成在不正確,因爲我們需要乘以背景與NSTimeInterval移動背景,但不是與INT值 – anjani 2014-09-10 14:52:01

+0

如果你乘以_gamescore背景不會移動 – anjani 2014-09-10 14:52:41

+0

hemant可以ü請刪除我會盡力解決我的問題 – anjani 2014-09-24 15:17:54