1

In the gameScene background moving horizontally it's working fine but i want to move background slow and fast according to score increasing, i proceed this way with nstimeinterval changing but nothing i can see change the background moving slow and fast its just going normal, if this way is wrong someone help me please.

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];
anjani
  • 165
  • 2
  • 12

1 Answers1

0

Try this code:

it will directly multiply animation time with score with _move*__gameScore and you can also provide some intalize value to _move variable.

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];
Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38