2014-11-05 46 views
1

我有一個很長的圖像,我想將其設置爲「Rollable」背景。 我試圖使用2個精靈實例,一旦圖像到達(frame - imageHeight)添加上面的第二個,並保持它的平穩運動。使用SKAaction的無限水平背景ios

但是,我收到了意想不到的行爲 - 在動畫的最後部分,屏幕重疊 - 第二個屏幕與第一個屏幕重疊。

這是我的代碼:

- (void) repositionBackground{ 

SKSpriteNode *currentBackground = self.backgroundArray[self.currentBackgoundIndex]; 
if(!currentBackground.parent && !self.currentBackgoundIndex) //First time running 
{ 
    SKAction *imageMinusFrameAnimation = [SKAction moveToY:-currentBackground.size.height+self.frame.size.height/2.0f duration:self.backgroundAnimationLength]; 
    SKAction *sequence = [SKAction sequence:@[imageMinusFrameAnimation, [SKAction runBlock:^{ 
     [self repositionBackground]; 
    }]]]; 
    [currentBackground runAction:sequence]; 
    [self addChild:currentBackground]; 

    NSLog(@"Calling Reposition for the first time"); 
} 
else 
{ 
    self.currentBackgoundIndex = (self.currentBackgoundIndex + 1) % [self.backgroundArray count]; 
    SKSpriteNode *newBackground = self.backgroundArray[self.currentBackgoundIndex]; 
    //Reposition th enew backfround on top of the current one 
    [newBackground setPosition:CGPointMake(0, self.frame.size.height/2.0f)]; 

    //Add actions to backfround - the top backgound to be vanished from the screen and the new one to appear and start scrolling down 
    SKAction *imageMinusFrameAnimation = [SKAction moveToY:-currentBackground.size.height+self.frame.size.height/2.0f duration:self.backgroundAnimationLength]; 
    SKAction *sequence = [SKAction sequence:@[imageMinusFrameAnimation, [SKAction runBlock:^{ 
     [self repositionBackground]; 
    }]]]; 
    [newBackground runAction:sequence]; 


    if(!newBackground.parent) 
    { 
     [self addChild:newBackground]; 
    } 
    float newBackgroundAnimationLength = self.frame.size.height/BACKGROUND_ANIMATION_VELOCITY; 
    SKAction *frameLengthAnimation = [SKAction moveByX:0 y:-self.frame.size.height duration:newBackgroundAnimationLength]; 

    [currentBackground runAction:frameLengthAnimation]; 


} 

}

+0

的[雪碧試劑盒側滾動]可能重複(http://stackoverflow.com/questions/19349168/sprite-kit-side-scrolling ) – rickster 2014-11-05 23:56:42

回答

0

我找出自己。 這是算法中的一個錯誤。 我張貼在這裏的溶液:(SpriteKit)

//creating 2 background - positioning one above the screen and one to stand on the (0.5,0} axis 
SKSpriteNode *firstBackground = [SKSpriteNode spriteNodeWithImageNamed:@"mtImage.jpeg"]; 
firstBackground.anchorPoint = CGPointMake(0.5f,0); 
firstBackground.zPosition = 1; 
SKSpriteNode *secondBackground =[firstBackground copy]; 
self.backgroundArray = @[firstBackground,secondBackground]; 
[firstBackground setPosition:CGPointMake(0, -self.frame.size.height/2)]; 
[secondBackground setPosition:CGPointMake(0, self.frame.size.height/2)]; 
self.currentBackgoundIndex = 0; 
self.backgroundAnimationLength = firstBackground.size.height/BACKGROUND_ANIMATION_VELOCITY; 

[self repositionBackground]; 


- (void) repositionBackground{ 

    SKSpriteNode *currentBackground = self.backgroundArray[self.currentBackgoundIndex]; 
    SKAction *majorPartAnimation; 
    //First time running 
    if(!currentBackground.parent && !self.currentBackgoundIndex) 
    { 
     float firstAnimationDuration = (currentBackground.size.height - self.frame.size.height)/BACKGROUND_ANIMATION_VELOCITY; 
     majorPartAnimation = [SKAction moveToY:-currentBackground.size.height+self.frame.size.height/2.0f duration:firstAnimationDuration]; 
     SKAction *sequence = [SKAction sequence:@[majorPartAnimation, [SKAction runBlock:^{ 
     [self repositionBackground]; 
     }]]]; 
     [currentBackground runAction:sequence]; 
     [self addChild:currentBackground]; 
    } 
    else 
    { 
     //current background needs to move by frameHeight and the new one has to be set above him and start its animation 
     majorPartAnimation = [SKAction moveToY:-currentBackground.size.height+self.frame.size.height/2.0f duration:self.backgroundAnimationLength]; 
     self.currentBackgoundIndex = (self.currentBackgoundIndex + 1) % [self.backgroundArray count]; 
     SKSpriteNode *newBackground = self.backgroundArray[self.currentBackgoundIndex]; 
     //Reposition th enew backfround on top of the current one 
     [newBackground setPosition:CGPointMake(0, self.frame.size.height/2.0f)]; 

     //Add actions to backfround - the top backgound to be vanished from the screen and the new one to appear and start scrolling down 
     SKAction *sequence = [SKAction sequence:@[majorPartAnimation, [SKAction runBlock:^{ 
      [self repositionBackground]; 
     }]]]; 
     [newBackground runAction:sequence]; 
     if(!newBackground.parent) 
     { 
      [self addChild:newBackground]; 
     } 
     float currentBackgroundAnimationLength = self.frame.size.height/BACKGROUND_ANIMATION_VELOCITY; 
     SKAction *minorPartAniamiton = [SKAction moveByX:0 y:-self.frame.size.height duration:currentBackgroundAnimationLength]; 
     [currentBackground runAction:minorPartAniamiton]; 
    } 

}