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