Duck
Duck

Reputation: 35953

Not working transitions in SpriteKit

I have a scene that is calling the next one using a transition like this:

  SKTransition *transition = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:0.5];

  SKView *skView = (SKView *)self.view;

  SKScene * scene = [[GameOverScene alloc] initWithSize:self.size];
  scene.scaleMode = SKSceneScaleModeAspectFill;
  [skView presentScene:scene transition:transition];

The elements that compose GameOverScene (buttons, images, etc.) are added on its init method.

The problem is that the transition is not seen. One scene immediately cuts to the other one.

I guess that transition happens before the next scene has a chance to build its elements. I have tried to move the creation of the next scene to didMoveToView without success.

For test purposes I have tried to delay the presentScene line in times even bigger than 2 seconds. When I do that I barely see the end frames of the transition.

How do I do that? What is the correct way of building the next scene and doing a transition that works.

Upvotes: 4

Views: 1269

Answers (2)

CloakedEddy
CloakedEddy

Reputation: 1995

If the new scene is particular resource heavy, i.e., requires a lot of texture loading, that will delay any frame rendering. If the texture loading takes longer than your transition time, you will miss all of it because the first frame that will be displayed has been rendered after your transition is finished. I ran into this as well, although I was better able to determine the root cause because I had a transition of 2 seconds of which only the last 0.5 seconds were shown.

How to fix this? Preload your textures. See Apple Docs.

+ (void)preloadTextures:(NSArray *)textures withCompletionHandler:(void (^)(void))completionHandler

I should emphasize the differences between SKSprite, SKTexture and your "image.png". SKSprite draws itself based on its texture property (or background color), and size. You can use one SKTexture for many sprites. In turn, an image file ("image.png") can supply multiple SKTexture objects.

Important: you want to use textures from the array of SKTexture objects passed to the method above to actually benefit from the texture loading. This will require some form of texture management.

If your problem is indeed texture related, let me know if you need me to expand on the texture management. A related post (which may be a bit dry) can be found here: SO sktexture-preloading.

Upvotes: 3

Rafał Sroka
Rafał Sroka

Reputation: 40030

I wrote exactly the same code in my app to show the game over scene with one minor difference. My code is as follows:

- (void)showGameOverScene
{
    SKTransition* reveal = [SKTransition doorsCloseVerticalWithDuration:0.5];

    SKScene* gameOverScene = [[RSGameOverScene alloc] initWithSize:self.size];

    [self.view presentScene:gameOverScene transition: reveal];
}

And it's been working like a charm. The only difference is that I am not calling:

scene.scaleMode = SKSceneScaleModeAspectFill;

In my RSGameOverScene I set up nodes in -(id)initWithSize:(CGSize)size, and again there is no problems. Everything just works.

I can't see a reason why this line could be a source of a problem but you might want to try commenting it out to see if it helps. However your code looks ok so the source of the problem can be somewhere else.

Upvotes: 0

Related Questions